file_get_contents, HTTP request failed

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 09:22:21

问题


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.



来源:https://stackoverflow.com/questions/22314753/file-get-contents-http-request-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!