我尝试用PHP从任何URL读取内容。这是我的代码(localhost):
$f=file_get_contents('http://www.youtube.com');
但我收到的信息是:
无法打开流:由于关联方在一段时间后未做出适当回应,或建立的连接失败,因为连接的主机未能回应
当我打开http://www.youtube.com/直接来说,这很好。所以我的联系没有错。这个故障是由我的php配置还是其他原因造成的?
我只想推荐cURL
。cURL
是比file_get_contents
更好的网站解析方法。
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
http://php.net/manual/en/book.curl.php
http://www.sitepoint.com/using-curl-for-remote-requests/
cURL不需要任何配置。有些网络主机会阻塞函数filesget_contents,所以cURL每次都是最好的。
为什么file_get_contents对此无效您可以阅读SO ans.
为什么不';文件集内容是否有效?
这里是:
<?php
echo file_get_contents('http://www.youtube.com');
?>
它在chrome和firefox中都很完美,我不知道你怎么了?再次检查。。。。