通过代理使用curl使用php

时间:2012-02-18 23:27:24

标签: php curl proxy

任何想法我的代码出错了...我试图通过代理与php中的curl函数连接...我假设代理工作bc我尝试了一些来自此列表http://hidemyass.com/proxy-list/search-234921但是似乎没有任何功能正常...

思想?

function my_fetch($url,$user_agent='Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)')
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, '75.74.244.122:1523');
$data = curl_exec();
curl_close($ch);
return $result;
}

2 个答案:

答案 0 :(得分:0)

它看起来不像您正在使用的代理正在运行:

jasonfunk@jasonfunk-laptop:$ telnet 75.74.244.122 1523
Trying 75.74.244.122...
telnet: Unable to connect to remote host: Connection refused

答案 1 :(得分:0)

您可以使用此脚本逐个使用随机尝试多个代理

获取随机代理

  function get_random_proxy(){
  srand ((double)microtime()*1000000);
  $f_contents = file ("proxy.txt");
  $line = $f_contents[array_rand ($f_contents)];
  return $line;
  }

随机使用一个代理调用curl函数

function get_curl_proxy($url){

$proxy_ip = get_random_proxy();
$agent = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.4 (KHTML, like Gecko) Chrome/4.0.233.0 Safari/532.4";
$referer = "http://www.google.com/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

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

return $data;
}

有待进一步参考,请参阅此内容 http://altafphp.blogspot.in/2012/06/using-proxies-with-curl-in-php.html