PHP Curl不适用于代理

时间:2016-07-21 12:03:39

标签: php curl proxy

拥有以下代码:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, $url);

$pageContents = curl_exec($ch);

echo curl_error($ch);
curl_close($ch);

我已经尝试了所有CURLOPT的组合,不知怎的,代理似乎没有用。如果我用curl_setopt注释掉这一行($ ch,CURLOPT_PROXY,$ proxy)它可以正常工作。我做错了什么?

1 个答案:

答案 0 :(得分:-1)

使用以下代码。

$url = 'URL';
$proxy = '127.0.0.1:8888';
//$proxyauth = 'user:password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_page = curl_exec($ch);
curl_close($ch);

echo $curl_page;

要禁用代理,只需将其设置为null。

curl_setopt($ch, CURLOPT_PROXY, null);