【问题标题】:PHP file_get_contents() is not working and showing gethostbyname failed error. How to fix it?PHP file_get_contents() 不工作并显示 gethostbyname failed 错误。如何解决?
【发布时间】:2021-02-28 17:07:04
【问题描述】:

我想用 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);

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

【问题讨论】:

  • editvar_dump(ini_get("allow_url_fopen")); 的输出包含在您的问题中。

标签: php web-crawler


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-08-18
    • 2020-03-16
    • 1970-01-01
    • 2016-08-14
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多