【发布时间】: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 让它工作非常感谢。随意发帖,不喜欢它!