【问题标题】:Using proxy, using curl, working on localhost but not on server使用代理,使用 curl,在本地主机上工作,但不在服务器上
【发布时间】:2011-08-12 20:21:39
【问题描述】:

我使用 squid 在我的一个 vps 上设置了一个代理,然后我制作了这个代理脚本,它在 wamp 和 localhost 上运行良好,但是当我将它上传到我的服务器时它不起作用,我似乎无法弄清楚为什么。

$proxy = "xx.xx.xxx.xx:3128";
$proxy = explode(':', $proxy);
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/1.0 (googlebot@googlebot.com http://googlebot.com/)');
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8" ); 
curl_setopt($ch, CURLOPT_REFERER, "http://tamilwin.com/"); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec ($ch);

提前致谢。

更新

我在服务器上遇到的错误是cURL error number:7cURL error:couldn't connect to host

【问题讨论】:

  • 它不工作?您如何得到什么错误或响应?
  • 我得到的错误是 cURL 错误号:7cURL 错误:无法连接到主机
  • 这意味着 curl 无法连接到该 IP 上的那个端口
  • 那是因为你想骗人

标签: php curl proxy vps


【解决方案1】:

代理主机可能无效。 尝试 telnet xx.xx.xxx.xx 3128(从您的本地 PC) 能连接成功吗?

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,在本地主机上运行良好,但在我的服务器上却不行。我写了一个检测脚本,让你知道它是否有效。

    <?php
    
    //$url = 'https://www.google.com';
    // to check your proxy
    $url = 'http://whatismyipaddress.com/';
    $proxy = 'x.x.x.x:3128';
    
    // create curl resource
    $ch = curl_init();
    
    // set options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true ); // read more about HTTPS http://stackoverflow.com/questions/31162706/how-to-scrape-a-ssl-or-https-url/31164409#31164409
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, file_exists('\cacert.pem'));
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy);
    
    
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    
    // $output contains the output string
    $output = curl_exec($ch);
    
    // close curl resource to free up system resources
    curl_close($ch); 
    
    //echo $output;
    
    $check = stripos($output,'</html>');
    // if there was a match in the stripos (string postion) function echo that the
    //proxy got the data and works
    if($check > 0)
    {
    echo $proxy." Works!
    ";
    // or else echo it doesn't work
    }else{
    echo $proxy." Is Dead!
    ";
    }
    
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多