首先,我是 CURL 新手,但不是 PHP 新手。
我有以下代码。
当 $url 位于本地主机时,这可以正常工作,但当 url 是我的实时服务器时,则失败。我查过与标题类似的问答据我所知,我不在代理服务器后面。我可以使用多种浏览器中的任何一种完美地访问“实时”网址。我有几个“开发”本地主机站点,它们都从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;
}
Run Code Online (Sandbox Code Playgroud)
返回的错误消息是:curled No cURL data returned for http://example.com/ [0] 无法连接到 example.com 端口 80: 访问错误
如果我强迫它使用 file_get_contents() 它也会完全崩溃:警告:file_get_contents(http://example.com/):无法打开流:尝试以访问权限禁止的方式访问套接字。在 D:\WEB...\cWeb.class.php 第 xxx 行
任何帮助表示赞赏 - 我几乎已经用尽了所有其他方法。有什么方法可以确定我是否在这台电脑上设置了代理。(我确实安装了诺顿,但没有安装其他任何东西,正如我所说,浏览器(Firefox、IE、Chrome)都可以看到实时站点。
提前致谢。