File_get_contents HTTP Request Error

I wrote simple PHP code to get some url content but it doesn't work it returns this error

file_get_contents ( http://www.nature.com/nature/journal/v508/n7496/full/nature13001.html ) [function.file-get-contents]: Failed to open stream: HTTP request failed! HTTP / 1.0 401 Unauthorized

here is my code. please help me.tnx

$content=file_get_contents('http://www.nature.com/nature/journal/v508/n7496/full/nature13001.html');
echo $content;

      

+3
article php http-status-code-401


source to share


2 answers


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 ,"");

one if you are facing a coding issue.

+4


source to share


  • Open the page with a browser and with the console open, check that the server is indeed sending 401s even when the page is being submitted and viewed

  • In php, open url in an alternative way to ignore the error (see http://php.net/manual/en/context.http.php )

  • You will also notice that this is gzip-encoded, see http://php.net/manual/en/function.gzinflate.php



Happy hack!

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics