code examples / proxycheck.io

Below you'll find code examples written by both us and 3rd party developers which enable you to integrate the proxycheck.io API with your software and services. If you've written a publicly available implementation of the proxycheck.io API such as a code example, function, class or plugin please contact us and we'll be happy to include it below. You can find ready to use plugins on our plugins page.


PHP Code

Adding support for our API in your website is very easy with PHP. Simply add this to the top of your PHP files for Login or Registration forms or wherever you need proxies blocked. If you would like a more advanced PHP example utilising a reusable function please view this example for developers on our GitHub page.

<?php

  // If you have an API key you should enter it below between the quotes
  // example: $API_Key = "111111-222222-333333-444444";
  $API_Key = ""; // Supply your API key between the quotes if you have one  
  $VPN = "0"; // Change this to 1 if you wish to perform VPN Checks on your visitors
  $Tag = "1"; // This setting tags your queries with a message you can view in your dashboard stats
    
  // By default the tag used is your querying domain and the webpage being accessed
  // However you can supply your own descriptive tag or disable tagging altogether.
  if ( $Tag == 1 ) {
    $Tag = htmlspecialchars($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
  } else {
    $Tag = "";
  }
  
  // If you're using CloudFlare change $_SERVER["REMOTE_ADDR"] to $_SERVER["HTTP_CF_CONNECTING_IP"]
  $visitor_ip = $_SERVER["REMOTE_ADDR"]; // Put visitor IP Address in a variable for later use

  // Querying the API through the non-TLS address (this is faster)
  $json = file_get_contents('http://proxycheck.io/v2/' . $visitor_ip . "?key=$API_Key" . "&vpn=$VPN" . "&tag=$Tag", true);

  // Decode the JSON from our API
  $json_decoded = json_decode($json);

  // Check if the IP we're testing is a proxy server
  if ( $json_decoded->$visitor_ip->proxy == "yes" ) {

    // If this is a proxy server we display the message below and exit the script
    // This will stop the rest of the page loading to this visitor
    echo "Please turn your Proxy Server off and try our website again.";
    exit;
    
  }

?>

If you would like a more advanced example utilising a reusable function and curl you can find it on our GitHub here

curl

Accessing our API through curl either by commandline or within a language such as PHP is very easy. All our API responses are in JSON so you will need to parse those responses but you can call our API from curl with the command below.

$ curl "http://proxycheck.io/v2/8.8.8.8"

When you do so you should receive a response like so:

{
    "8.8.8.8": {
        "proxy": "no",
        "provider": "GOOGLE - Google LLC, US"
    }
}
C# Code

Developer hollow87 has made available a nuget package for proxycheck.io at nuget.org available here thanks hollow87! - We've included some install commands for popular clients below for your convenience.

Install-Package ProxyCheck -Version 1.0.0
dotnet add package ProxyCheck --version 1.0.0
paket add ProxyCheck --version 1.0.0

You can download a C# console app / assembly from GitHub here made by developer hollow87. We thank him for this valuable contribution. For your convenience we have included the code below.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ProxyCheck
{
    public class ProxyCheck
    {
        public class ProxyCheckResposne
        {
            [JsonProperty(PropertyName = "node")]
            public string Node { get; internal set; }

            [JsonProperty(PropertyName = "asn")]
            public string ASN { get; internal set; }

            [JsonProperty(PropertyName = "provider")]
            public string Provider { get; internal set; }

            [JsonProperty(PropertyName = "country")]
            public string Country { get; internal set; }

            [JsonProperty(PropertyName = "ip")]
            public string IP { get; internal set; }

            public IPAddress IPAddress => IPAddress.Parse(IP);

            [JsonProperty(PropertyName = "proxy")]
            public string Proxy { get; internal set; }

            public bool IsProxy => Proxy.ToLower() == "yes";

            [JsonProperty(PropertyName = "type")]
            public string Type { get; internal set; }

            [JsonProperty(PropertyName = "query time")]
            public string QueryTime { get; internal set; }

            [JsonProperty(PropertyName = "error")]
            public string Error { get; internal set; }
        }

        private const string PROXYCHECKURL = "proxycheck.io/v1";

        /// 
        /// The API key to use with the query
        /// 
        public string ApiKey { get; set; } = "";

        /// 
        /// Including checking for VPN
        /// 
        public bool IncludeVPN { get; set; }

        /// 
        /// Use HTTPS when checking IP address (slower)
        /// 
        public bool UseTLS { get; set; }

        /// 
        /// Enables viewing the ASN of the network the IP address belongs to
        /// 
        public bool IncludeASN { get; set; }

        /// 
        /// Includes the answering node in the reply
        /// 
        public bool IncludeNode { get; set; }

        /// 
        /// Includes the time it took for query
        /// 
        public bool IncludeTime { get; set; }

        public async Task IsProxyAsync(IPAddress ipAddress, string tag = "")
        {
            try
            {
                var response = await QueryAsync(ipAddress, tag);

                if (!string.IsNullOrWhiteSpace(response.Error))
                    throw new Exception(response.Error);

                return response.IsProxy;
            }
            catch(Exception e)
            {
                // Just rethrowing the exception
                throw;
            }
        }

        public async Task QueryAsync(IPAddress ipAddress, string tag = "")
        {
            var url = new StringBuilder()
                .Append($"{(UseTLS ? "https://" : "http://")}{PROXYCHECKURL}/{ipAddress}")
                .Append(!string.IsNullOrWhiteSpace(ApiKey) ? $"&key={ApiKey}" : "")
                .Append($"{(IncludeVPN ? "&vpn=1" : "")}")
                .Append($"{(IncludeASN ? "&asn=1" : "")}")
                .Append($"{(IncludeNode ? "&node=1" : "")}")
                .Append($"{(IncludeTime ? "&time=1" : "")}");

            using (var client = new HttpClient())
            {
                FormUrlEncodedContent content = null;
                
                if (!string.IsNullOrWhiteSpace(tag))
                {
                    content = new FormUrlEncodedContent(new Dictionary
                    {
                        {"tag", tag}
                    });
                }

                try
                {
                    HttpResponseMessage response = null;
                    if (content != null)
                        response = await client.PostAsync(url.ToString(), content);
                    else
                        response = await client.GetAsync(url.ToString());

                    string json = await response.Content.ReadAsStringAsync();

                    ProxyCheckResposne result = JsonConvert.DeserializeObject(json);

                    return result;
                }
                catch(Exception e)
                {
                    // Should do something here just rethrowing
                    throw;
                }
            }

        }
    }
}

Node.js

Accessing the API with Node.js is very easy as shown by the code snippet below.

const request = require('request')

request('http://proxycheck.io/v2/8.8.8.8', function(error, res, body) {
  console.log(JSON.parse(body))
})

When you do so you should receive a response like so:

{
    "8.8.8.8": {
        "proxy": "no",
        "provider": "GOOGLE - Google LLC, US"
    }
}
Python

Accessing the API with Python is very easy as shown by the code snippet below.

requests.get('http://proxycheck.io/v2/8.8.8.8')

When you do so you should receive a JSON response that you will need to parse like so:

{
    "8.8.8.8": {
        "proxy": "no",
        "provider": "GOOGLE - Google LLC, US"
    }
}
Ruby on Rails

Jonathan has been kind enough to create a gem for utilising the proxycheck.io API and you can find it on the rubygems.org website here thanks jonathan! - We've included the gem install command below for your convenience.

gem install proxycheck

Below we've included a simple way to query the API with Ruby.

require 'json'
require 'open-uri'

JSON.parse(open('http://proxycheck.io/v2/8.8.8.8').read)

When you do so you should receive a JSON response like so:

{
    "8.8.8.8": {
        "proxy": "no",
        "provider": "GOOGLE - Google LLC, US"
    }
}

Back
Last update: 2nd of Jan 2018.2nd of January 2018: Most example code was updated to use the new v2 API and the CSS styling was updated.

10th of November 2017: New examples were added for curl, Node.js, Python and Ruby, layouts were also updated.

13th of September 2017: The page apperance was updated with a new tab view for the different examples, this is getting ready for a wider selection of examples coming soon. A C# example was also added.

26th of June 2017: Three new minecraft plugins were added to the page.

25th of May 2017: The PHP Example was updated to include our new query tagging feature.

7th of April 2017: A better PHP example utilising functions and curl on GitHub was linked to from this page.

1st of April 2017: Information about a 3rd party Minecraft plugin which uses our API was added to the page.

Advertisement: