How to use our proxies in PHP code

Proxies can be used in PHP code as long as the CURL library is enabled.

If you have a script that needs to send traffic via a Proxy Server, one of the best options is to use PHP's cURL library.

Create a file called test_proxy.php:

function getUrlContents($url, $proxy = NULL, $post_data = NULL, $timeout = 30, $userpass = NULL) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    if ($post_data !== NULL) {
        curl_setopt($curl, CURLOPT_POST, TRUE);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    }
    if ($proxy != NULL) {
        curl_setopt($curl, CURLOPT_PROXY, $proxy);
    }
    if ($userpass != NULL) {
        curl_setopt($curl, CURLOPT_USERPWD, $userpass);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE);
    $contents = curl_exec($curl);
    curl_close($curl);

    return $contents;
}

$proxy = '1.1.1.1:1212';  // <-- Replace 1.1.1.1:1212 with your proxy IP:PORT
$url = 'http://ip-api.com';
$content = getUrlContents($url, $proxy);
echo $content;

Upload and execute test_proxy.php

-- If it works, you should see the contents of the specified $url
-- If you get an access denied page, ensure your server's IP is added as an authorized IP in our Control panel
-- If you see CURL errors, contact your web host about CURL support

complete at 0.0952 s; cached: 1; cache enable: truek716d4d1d20e0660360b395ec6449b85aafd9d8a4.html