Using proxy without cURL

1.8k Views Asked by At

I have tried to use proxy in php using both the methods. I have socks5 proxies. Which works great when I use cURL method. But when I try to use stream_context_set_default it doesn't work at all. Is there any way to use proxies ? For example : this proxy 207.174.151.226:45021doesn't work with stream_context_set_default but works with cURL. I don't want to use proxy using cURL because I need proxy everywhere in my php file. Any help regarding this ? Code :

<?php
$default_opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar",
    'proxy'=>"186.34.59.99:10000"
  )
);

$default = stream_context_set_default($default_opts);

$homepage = file_get_contents('http://www.google.com');
echo $homepage;


?>

This works :

<?php
$url = "http://www.google.co.uk";
$ch = curl_init();

if($ch === false)
{
    die('Failed to create curl object');
}

$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

//proxy details
curl_setopt($ch, CURLOPT_PROXY, '186.34.59.99:10000');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

$data = curl_exec($ch);
curl_close($ch);

echo $data;

?>

Thanks!

1

There are 1 best solutions below

1
On

Make sure you send the URI including the original hostname. You can achieve this by explicitly passing request_fulluri = true as part of $default_opts['http'] as described on http://php.net/manual/en/context.http.php