【问题标题】:cURL times out when online but not on localhostcURL 在线时超时,但不在本地主机上
【发布时间】:2015-01-02 08:09:51
【问题描述】:

我有这个 curl 代码,应该可以让我从 411.com 获取文本。 当我在本地主机上使用 wamp 进行测试时,代码运行良好,但是当我在线测试时,出现错误连接超时。

我的网站使用 ssl。不确定这是否重要。

<?php     
//$url = "http://www.411.com/phone/1-310-402-9829";
$url = "www.411.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36'); 
$output = curl_exec($ch);

if(curl_errno($ch))
{
    echo 'error:' . curl_error($ch);
}

curl_close($ch);

function extract_unit($string, $start, $end)
{
$pos = stripos($string, $start);

$str = substr($string, $pos);

$str_two = substr($str, strlen($start));

$second_pos = stripos($str_two, $end);

$str_three = substr($str_two, 0, $second_pos);

$unit = trim($str_three); // remove whitespaces

return $unit;
}

$unit = extract_unit($output, '<span class="subtitle block pull-left">', 'in');

// Outputs: acronym
echo $unit;
?>

【问题讨论】:

  • 您的托管服务可能有阻止连接的防火墙。
  • @Barmar 我刚刚联系了我的托管服务,他们告诉我 411.com 正在阻止他们的 IP。我该怎么办?使用代理?
  • 是的,这可能会奏效。
  • @Barmar 让它工作非常感谢。随意发帖,不喜欢它!

标签: php curl localhost


【解决方案1】:

问题是我的网站无法访问 411.com。

我在 curl 中添加了一个代理,然后我能够成功地抓取页面。

curl_setopt($ch, CURLOPT_PROXY, $proxy);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2020-12-18
    • 2012-11-17
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多