file_get_contents HTTP request failed

二次信任 提交于 2019-12-01 20:16:26

Here's an alternative to file_get_contents using cURL:

$url = 'http://www.example.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);

You might want to add this curl_setopt($curl, CURLOPT_ENCODING ,""); if you encounter encoding problem.

  1. Open the page using your browser and with the console open, see that the server does indeed send a 401 even when page is sent and viewable

  2. On php, open the url in an alternate way to ignore the error (see http://php.net/manual/en/context.http.php)

  3. You'll also notice that it's gzip-encoded, see http://php.net/manual/en/function.gzinflate.php

Happy hacking!

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