如何通过curl_init使用代理?

时间:2017-06-14 03:39:13

标签: php curl proxy

<?php
$url = 'http://www.php.net/';
$proxy = '104.41.154.213:8118';
//$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_HTTPPROXYTUNNEL, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);




if (preg_match('<body class="home ">', $data)) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

当我停止使用代理但不使用任何代理时,它正在工作。有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:0)

您需要为卷曲设置proxytype

curl_setopt($ch,CURLOPT_PROXYTYPE,xxxxxxx);

您还可以使用

调试卷曲错误
if($data === false)
  echo 'Curl error: ' . curl_error($ch);