PHP-文件\u获取\u内容无法打开流:连接已关闭?
实现(url是有效且正在运行的url): 我正在用php编写一个爬虫程序,有时file_get_contents会返回以下错误: 无法打开流:连接已关闭PHP-文件\u获取\u内容无法打开流:连接已关闭?,php,file-get-contents,Php,File Get Contents,实现(url是有效且正在运行的url): 我正在用php编写一个爬虫程序,有时file_get_contents会返回以下错误: 无法打开流:连接已关闭 这并不总是发生,所以当它发生的时候,我有点困惑。这是我这边的错误还是我正在爬行的网站?无论采用哪种方法,都可以一直重试直到错误不再发生,还是有更好的方法?您需要为此创建一个流 阅读 使用php CURL库 为了更好地管理客户端请求。。由于主机服务器上的安全限制,file_get_contents()函数失败 function url_get_
这并不总是发生,所以当它发生的时候,我有点困惑。这是我这边的错误还是我正在爬行的网站?无论采用哪种方法,都可以一直重试直到错误不再发生,还是有更好的方法?您需要为此创建一个流 阅读
使用php CURL库
为了更好地管理客户端请求。。由于主机服务器上的安全限制,file_get_contents()函数失败
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
我想这可能会有帮助。为什么你认为这会有帮助?你应该检查这个链接。我看了一下,这是一个不同的错误,我担心:(谢谢,我应该早点使用cURL。我使用的是谷歌应用程序引擎,所以不确定它是否可用。至少目前所有警告都已停止。
<?php
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents($url, false, $context);
?>
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}