PHP file_get_contents 返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14801955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:59:36  来源:igfitidea点击:

PHP file_get_contents returning false

phpjson

提问by Mrk Fldig

Maybe i've been sat here too long staring at this but WHY would file_get_contents return false here, i've cut and paste the URL and it works fine?

也许我已经坐在这里盯着这个看太久了,但为什么 file_get_contents 会在这里返回 false,我已经剪切并粘贴了 URL 并且它工作正常?

     $url = "http://jobs.github.com/positions.json?search=" . $as_any . "&location=" .       $location;
     // URL contains http://jobs.github.com/positions.json?search=Project Manager&location=London
     var_dump($url);
     $json = file_get_contents($url);
     var_dump($json);
     $results = json_decode($json, TRUE);
     var_dump($results);
     exit;

Thanks

谢谢

Marc

马克

EDIT:

编辑:

I Have checked for allow_url_fopen and its definately on.

我已经检查过 allow_url_fopen 并且它肯定是打开的。

采纳答案by Barmar

Try this:

尝试这个:

$query = http_build_query(array('search'=>$as_any, 'location'=>$location));
$url = "http://jobs.github.com/positions.json?" . $query;

The problem is that you weren't URL-encoding the search term, which contains a space. The request was returning a 400 error from the server, which you'd have noticed if you had error reporting enabled.

问题是您没有对包含空格的搜索词进行 URL 编码。该请求从服务器返回 400 错误,如果您启用了错误报告,您就会注意到这一点。

回答by Julio

You may need to enable allow_url_fopen in your php.ini

您可能需要在 php.ini 中启用 allow_url_fopen

http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

From the documentation:

从文档:

This option enables the URL-aware fopen wrappers that enable accessing URL object like files.

此选项启用支持访问 URL 对象(如文件)的 URL 感知 fopen 包装器。

Your server may be preventing you from opening a file located at a URL using file_get_contents.

您的服务器可能阻止您使用 file_get_contents 打开位于 URL 处的文件。

回答by Emery King

Probably allow_url_fopen in your php.ini: http://php.net/manual/en/filesystem.configuration.php

可能在您的 php.ini 中使用 allow_url_fopen:http: //php.net/manual/en/filesystem.configuration.php

Needs to be allowed.

需要被允许。

回答by vencedor

Sometimes if file_get_contentsreturn false(not found) you should see if the url is encoded.

有时,如果file_get_contents返回false(未找到),您应该查看 url 是否已编码

example:

例子:

http://test.net/демо/

http://test.net/демо/

Should be:

应该:

https://test.net/%D0%B4%D0%B5%D0%BC%D0%BE/

https://test.net/%D0%B4%D0%B5%D0%BC%D0%BE/