Vladimir Danoski
Vladimir Danoski

Reputation: 83

Fixing error with file_get_contents permission denied

I have a question connected with JSON and PHP. So, if you visit this site: http://air.moepp.gov.mk/graphs/site/pages/MakeGraph.php?station=SkopjeRegion&parameter=PM10D&beginDate=2018-02-10&beginTime=19:00&endDate=2018-02-17&endTime=19:00&i=1518893212453&lang=en

As a return you get HTML, but if you go to the response, the response is pure JSON, so I'm trying to get the JSON data, but I fail. Probably I'm doing something wrong, but I don't know what. I tried with file_get_contents() and curl but I don't get anything from it... So here is what I'm trying to do:

<?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);

And as return I get NULL.

Edit:

I figured out that my PHP function file_get_contents() is not properly working because of the problem with the permission. The problem was:

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

After googling, and suggestion from @geoidesic, I found out that SELinux (Security Enhanced Linux) is blocking Apache (by default network connection from httpd (Apache) is disabled. So i followed step-by-step tutorial on http://drib.tech/programming/php-file_get_contents-not-working and finally got it to work. Anyways, here is the solution in short if someone needs it:

  1. Be sure you have allow_url_fopen=On in your php.ini file. (If someone doesn't know where your php.ini file is, quick check with phpinfo() could get you everything.

  2. Check status of SELinux:

    sudo sestatus

    If the status is enabled , then you can check whether the boolean flags httpd_can_network_connect and httpd_unified are enabled, set to 1:

    sudo sestatus -b | grep httpd_can_network_connect and sudo sestatus -b | grep httpd_unified

    If they're off, run this command to set them on:

    sudo setsebool -P httpd_can_network_connect 1 sudo setsebool -P httpd_unified 1

    And then reboot, or restart httpd. So that's all, again, thanks a lot to the guy/s at drib.tech.

Upvotes: 7

Views: 16113

Answers (4)

DigitalNinja
DigitalNinja

Reputation: 11

This was the answer for me Apache Server SELinux Boolean List this was set to off

httpd_can_network_connect --> off

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-managing_confined_services-the_apache_http_server-booleans

Upvotes: 0

HomeIsWhereThePcIs
HomeIsWhereThePcIs

Reputation: 1464

I had the same issue just now and it was caused by selinux.

Resolved by running:

sudo setsebool -P httpd_can_network_connect 1
sudo setsebool -P httpd_unified 1

Upvotes: 14

Elijah M
Elijah M

Reputation: 835

Use $content = readfile('url'); and there should be no errors.

I was having this problem too but that worked, and also readfile is faster than file_get_contents.

Upvotes: -1

geoidesic
geoidesic

Reputation: 5053

Check your PHP error log. Possibly you have a network issue where your PHP instance can't access that URL. Perhaps you're running it from a virtual environment that doesn't have external network access for example.

Try file_get_contents('google.com') for example and see what you get back.

I tried your code and it worked perfectly for me. I got JSON back.

I don't know why you say "If you visit this site... you get HTML" because you don't, you get JSON.

Upvotes: 0

Related Questions