首页 > 解决方案 > PHP file_get_contents() 不工作并显示 gethostbyname failed 错误。如何解决?

问题描述

我想用 PHP 做一个简单的网络爬虫。我尝试过使用 file_get_contents()。但它不能正常工作。它显示如下错误。

Warning: file_get_contents(): php_network_getaddresses: gethostbyname failed. errno=0 in /storage/emulated/legacy/htdocs/search/crawl.php on line 12

Warning: file_get_contents(http://www.wikipedia.com): failed to open stream: php_network_getaddresses: gethostbyname failed. errno=0 in /storage/emulated/legacy/htdocs/search/crawl.php on line 12

Warning: implode(): Invalid arguments passed in /storage/emulated/legacy/htdocs/search/crawl.php on line 38

这是什么意思??第 12 行的代码也是

$mainUrl = "http://www.wikipedia.org";
$str = file_get_contents($mainUrl);

第 38 行的代码,

$all_links = implode(",",$sec_url);

那么现在如何解决这个问题?

标签: phpweb-crawler

解决方案


尝试使用 https 并确保在 php.ini 中允许:allow_url_fopen=1

$mainUrl = "https://samplesite.com";
$str = file_get_contents($mainUrl);

推荐阅读