file_get_contents, HTTP request failed

2019-02-19 16:40发布

问题:

I try to get content from another site by using file_get_contents but I always get "failed to open stream: HTTP request failed!" allow_url_fopen is on, and I have tested with my firewall off. But it still happens, what other reasons can it be?

EDIT: Here is the full message Warning: file_get_contents(http://yemto.deviantart.com/): failed to open stream: HTTP request failed! in D:\xampp\htdocs\deviantart\webcam\img.php on line 15

And code

//Load the page into a DOMDocument
$file = file_get_contents(strToLower("http://".$user.".deviantart.com/"));//TO-DO <-- if deivantart name is empty, it crashes
$doc = new DOMDocument();
@$doc -> loadHTML($file);

回答1:

Your webserver closes the connection immediately if the client doesn't send a User-Agent header. Try adding a user_agent option to your php.ini, or put:

ini_set('user_agent', 'PHP');

in your script.