首先我是CURL的新手,但不是PHP。
我有以下代码。
这个工作正常,$ url在localhost上,但是当url是我的实时服务器时失败。我检查了类似的Q& A到标题,据我所知,我不在代理服务器后面。 我可以访问" live" url完美地使用几种浏览器中的任何一种。 我有几个"开发" localhost站点,它们都从curl返回数据。没有等同的"生活"网站(或google.com,stackoverflow.com)似乎可以访问。所有都产生相同的错误信息。
#Note this is within a public function within a class so $this->currentword has been defined as a string
$url = "http://example.com/{$this->currentword}";
#$url = "http://example.localhost/{$this->currentword}"; // this works returns the content ok
$agent = "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";
if (function_exists('curl_version')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 45);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 15);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
#curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$html = curl_exec($curl);
$info = curl_getinfo($curl);
if ($html === false || $info['http_code'] != 200) {
$html = "<br />No cURL data returned for {$url} [". $info['http_code']. "]";
if (curl_error($curl)) $html .= "<br />\n".curl_error($curl);
}
curl_close($curl);
echo "<br />\ncurled";
} else if (file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
$options = array(
'http' => array(
'user_agent' => $agent,
'max_redirects' => 10,
'timeout' => 120,
)
);
$context = stream_context_create($options);
$html = @file_get_contents($url, false, $context);
if ($html !== false) { echo "works"; }
} else { echo 'You have neither cUrl installed nor allow_url_fopen activated. Please setup one of those!'; }
if (!empty($html)) {
$html = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $html);
echo $html;
}
返回的错误消息是: 卷曲 没有为http://example.com/ [0]返回cURL数据 无法连接到example.com端口80:访问不正确
如果我强制它使用file_get_contents(),它也会完全失败: 警告:file_get_contents(http://example.com/):无法打开流:尝试以其访问权限禁止的方式访问套接字。在第xxx行的D:\ WEB ... \ cWeb.class.php
任何帮助表示赞赏 - 其他所有人都非常疲惫。有没有办法确定我是否在这台PC上设置了代理。 (我确实安装了诺顿,但没有别的,因为我说浏览器(Firefox,IE,Chrome)都可以看到现场网站。
提前致谢。
答案 0 :(得分:2)
您确定Windows防火墙允许PHP / Apache连接到外部站点吗?暂时禁用以检查