php file_get_contents through proxy

If you want php file_get_contents to use proxy, you can use the following code:

$options = array(
    'http' => array(
        'proxy'           => 'tcp://localhost:3128',
        'request_fulluri' => true,
    ),
);
$cx = stream_context_create($options);

echo file_get_contents("https://www.google.com", false, $cx);

The http proxy server is localhost, the port of the proxy server is 3128. The server does not require authentication. Note that the options parameter of stream_context_create uses ‘http’ as the key regardless file_get_contents is connecting to an http or an https url. If your proxy requires authentication, you can use the method here to provide a username and a password.

If you cannot modify all the source code that contains file_get_content but want all of them to use proxy, you can do this:

  1. create a php file proxy.php in include path which contains the following content:
    <?php
    stream_context_set_default(['http'=>['proxy'=>'localhost:3128']]);
  2. edit /etc/php.ini to include a line auto_prepend_file = proxy.php. php auto_prepend_file option lets php load the specified file before loading main file as if the main file uses the require function to include that file, so every php project will get a default stream context that uses a proxy. Make sure the include path is accessible by your php project(website).
  3. restart httpd

The above method only works for file_get_contents and does not work for curl. To let php curl use proxy, you need to use the curl_setopt function before calling curl_exec as follows:

<?php
function getUrl($url)
{
    $ch = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXY, "http://localhost"); //your proxy url
    curl_setopt($ch, CURLOPT_PROXYPORT, "3128"); // your proxy port number
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:pass"); //username:pass
    $file_contents = curl_exec($ch);
    curl_close($ch);
    return $file_contents;
}
echo  getUrl("https://www.google.com");

The above example sets an http proxy for curl.

If you want to set once for all like  for file_get_contents, you can set the environment variable  http_proxy using putenv("http_proxy=localhost:3128"); in proxy.php. Alternatively, you can add the http_proxy environment variable in /etc/sysconfig/httpd:

http_proxy=localhost:3128

and restart httpd. The environment variable will be passed from apache to php and guide curl to use proxy. This works in CentOS. In other system such as Debian, you can set the environment variable in httpd.conf like:

SetEnv http_proxy “localhost:3128”

Or, set the environment variable in php.ini. Whatever method you use, make sure the environment variable is accessible in  php(you can try to get it using the getenv function). If you want curl functions to use proxy for https urls, you need to add the environment variable https_proxy=localhost:3128. This is not like the context option for file_get_contents.

 

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Leave a Reply