PHP file_get_contents返回 false

2022-08-30 17:11:20

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

$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;

编辑:

我已经检查过了,它肯定在。allow_url_fopen


答案 1

试试这个:

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

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


答案 2

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

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

从文档中:

此选项启用 URL 感知 fopen 包装器,这些包装器允许像文件一样访问 URL 对象。

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


推荐