无法通过PHP脚本访问文件,但可以在浏览器中打开它

I have an url with RSS feed:

$url = 'http://www.myurl.com/sth?format=RSS';

I can open it in a browser without a problem. But

$feed->load($url) 

returned 'false'. So I started investigating:

$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);           
print curl_error($ch); // prints 'couldn't connect to host'
echo "CODE: ".$retcode; // $retcode is 0

$file_headers = get_headers($file);
echo $file_headers[0]; // is null

So, what can be the reason for such behaviour? Is some port blocked on myurl.com server? Is there a way to work around it (like create local copy of the file and work on it)?

Probably the site has some block for external connections implemented, such check for User-Agent, referal...

maybe the server is doing some sniffing and doesnt serve anything on that url if it finds that curl is doing the work. you could try phantomJS and/or Selenium to get around such filters. Selenium has PHP bindings.

If you're on CentOS (known issue on that flavour), do the following to test and make sure that's the not issue. later you can later issue specific filtering.

> emacs  /etc/selinux/config

locate following line
SELINUX=enforcing

Change this to
SELINUX=disabled

save the file and try again. it could be your localhost firewall if you can open it in a browser without a problem.

if this is an issue, set SELinux back to enforcing and issue

setsebool -P httpd_can_network_connect

if you want httpd to be able to connect to tcp ports