问题
I have a PHP script that needs to process the same site's RSS feed. Specifically, I'm displaying the most recent blogs from the WordPress RSS feed on the home page. On our staging server it worked fine but on our live (which is a completely different, but LAMP) hosting environment it's not working.
I can run file_get_contents
or curl
on a remote url fine, but when I try to retrieve our own RSS feed, I am returned a 404 not found page. One other oddity, if I try file_get_contents(http://domain.com/test.txt)
it fails with a 404 but if I do file_get_contents(http://www.domain.com/test.txt)
I get the contents of the test text file. This is all assuming I'm running the script from domain.com (not www.domain.com)
I've setup an example here: http://bkwld.com/test.php
回答1:
Ok, I still don't know why the hell it's doing this, but I'm going to solve it by running my feed through feedburner and then parsing it's RSS feed. Because it's on a remote domain, it works in my tests. Not ideal, but w/e.
回答2:
I just had this similar issue. The DNS is the problem, it is not resolving your domain name. You should use the IP instead of the domain in your scripts.
You can ping your domain in cmd and use that IP.
回答3:
I had a very similar problem - you might try using 127.0.0.1 instead of your own domain name (assuming your apache setup doesn't prevent you doing that). Something to do with domain resolution I believe, quirk of the api.
回答4:
Indeed, odd. How will you parse the file? You can maybe load it into SimpleXML directly;
$xml = simplexml_load_file("http://domain.com/blog/feed/index.php");
/* Use Simple XML to parse the RSS feed */
I realize this approach does not solve your problem, you only use another approach - but maybe it's enough for you.
回答5:
If domain.com/test.txt returns 404 and www.domain.com/test.txt succeeds the host DNS might have hosted domains configured differently. In any case, I'm sure you already tried to retrieve the RSS feed with 'www,' but did you try with a relative path? eg: file_get_contents('../../feed/')
来源:https://stackoverflow.com/questions/940198/using-file-get-contents-or-curl-to-url-on-same-server