小编Vla*_*ski的帖子

修复 file_get_contents 权限被拒绝的错误

我有一个与 JSON 和 PHP 相关的问题。所以,如果你访问这个网站:http : //air.moepp.gov.mk/graphs/site/pages/MakeGraph.php?station=SkopjeRegion¶meter=PM10D&beginDate=2018-02-10&beginTime=19 : 00&endDate=2018-02- 17&endTime=19:00&i=1518893212453&lang=en

作为返回,您将获得 HTML,但是如果您转到响应,则响应是纯 JSON,因此我试图获取 JSON 数据,但失败了。可能我做错了什么,但我不知道是什么。我试着用file_get_contents()curl,但我不从它那里得到任何东西......因此,这里是我想要做的事:

<?php
$content = file_get_contents('http://air.moepp.gov.mk/graphs/site/pages/MakeGraph.php?station=SkopjeRegion&parameter=PM10D&beginDate=2018-02-10&beginTime=12:00&endDate=2018-02-17&endTime=12:00&i=1518865590001&lang=en');  
$contents = json_decode($content, true);  
var_dump($contents);
Run Code Online (Sandbox Code Playgroud)

作为回报,我得到NULL.

编辑:

我发现file_get_contents()由于权限问题,我的 PHP 函数无法正常工作。问题是:

file_get_contents('url'): failed to open stream: Permission denied in /var/www/html/xxx.php on line X

在谷歌搜索和@geoidesic 的建议之后,我发现 SELinux(安全增强型 Linux)正在阻止 Apache(默认情况下来自 httpd(Apache)的网络连接被禁用。所以我按照http://drib上的分步教程进行了操作.tech/programming/php-file_get_contents-not-working终于让它工作了。无论如何,如果有人需要,这里是一个简短的解决方案:

  1. 确保你allow_url_fopen=On的 php.ini 文件中有。(如果有人不知道您的 php.ini 文件在哪里,使用 phpinfo() 快速检查可以为您提供一切。

  2. 检查 SELinux 的状态:

    sudo sestatus

    如果状态为enabled,那么您可以检查布尔标志httpd_can_network_connecthttpd_unified …

php json curl

5
推荐指数
2
解决办法
6896
查看次数

标签 统计

curl ×1

json ×1

php ×1