file_get_contents HTTP request failed - php

I written simple php code to get some url content but it dosn't work
it return 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;

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.

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
On php, open the url in an alternate way to ignore the error (see http://php.net/manual/en/context.http.php)
You'll also notice that it's gzip-encoded, see http://php.net/manual/en/function.gzinflate.php
Happy hacking!

Related

file_get_contents request to external site

I am trying to do a file_get_contents of this Demo URL
However the server has trouble with getting data from external sites. This is the error I get if I echo the file_get_contents:
Found The document has moved
here. Apache/2.4 Server at spotifycharts.com Port 80
I have turned the register_global on in the php.ini file, but this doesn't help.
What would be the most logical thing to check to make sure my website is able to get data from external sites?
Just use the https url instead of the http url:
https://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200
You may need to request with cURL, I don't think file_get_contents() can follow 302 redirects.
Something like this should work...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if (preg_match('#Location: (.*)#', $a, $r))
$l = trim($r[1]);
How to get the real URL after file_get_contents if redirection happens?
Source

HTTP Authentication fails from PHP script on Wowza Server?

Is there a configuration/setting for the Wowza server that must be correct to allow HTTP Authentication?
According to How to get realtime connection counts from Wowza Media Server, I should be able to get some XML information that I need. If I go to the url from a browser, it prompts for username/password, and I get the data, but if I try to accomplish it from PHP it fails no matter what method I use:
HTTP request failed! HTTP/1.0 401 Unauthorized
Method #1:
$data = file_get_contents('http://admin:password#123.123.123.123:8086/connectioncounts');
Method #2:
$handle = fopen('http://admin:password#123.123.123.123:8086/connectioncounts', "r")
Method #3:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, http://123.123.123.123:8086/connectioncounts);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "admin:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
Try change CURLAUTH_BASIC to CURLAUTH_ANY

Response received from a url via php script is smaller than when I open it in browser

I'm trying to fetch this url via a script: http://api.alarabiya.net/sections/2/
But JSON response received is much smaller than when I open it directly in a browser,
please notice that I tried this url through CURL and set the same USER-AGENT of the browser and all request header used in the browser and I still get a smaller response.
Here's an exmaple using just file_get_contents
<?php
echo file_get_contents("http://api.alarabiya.net/sections/2/");
?>
My question is if there's a request size limit when using file_get_contents or if the PHP's memory can't handle it or what's the problem exactly?
When I CURLed this in shell it gave me the same o/p as in php (the trimmed output).
I finally found a solution for this:
$url = "http://api.alarabiya.net/sections/2/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
// This is what solved the issue (Accepting gzip encoding)
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$response = curl_exec($ch);
curl_close($ch);
echo $response;

Display of another authentication enabled page content?

How to display content of other page which has authentication enabled?
Warning: file_get_contents(http://abc/report.cmd?report=123): failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in //html/tracker/index2.php on line 13
$file_path = 'http://abc/report.cmd?report=123';
$content=file_get_contents($file_path);
echo $content;
Use the curl library to authenticate
<?php
// create a new cURL resource
$curl = curl_init();
// set URL and other appropriate options
curl_setopt($curl, CURLOPT_URL, "http://www.example.com/");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
// grab URL and pass it to the browser
curl_exec($curl);
// close cURL resource, and free up system resources
curl_close($curl);
?>
Authenticate first.., then request the request the content.
Here's an example of authenticating against HTTP Basic auth w/ PHP:
http://php.net/manual/en/features.http-auth.php

how to authenticate username/password of https site to access a file in php

I want to access an xml file from https://test24.highrisehq.com/tasks/upcoming.xml using php.
Following is the sample code:
$xml = simplexml_load_file("https://test24.highrisehq.com/tasks/upcoming.xml");
since the connection is secured I am getting an error:
Warning:
simplexml_load_file(https://test24.highrisehq.com/tasks/upcoming.xml)
[function.simplexml-load-file]: failed
to open stream: HTTP request failed!
HTTP/1.1 401 Unauthorized in
D:\wamp\www\xml\index.php on line 3
I need to put in the username and password somewhere in the code before accessing the file. Can anyone please tell he how can I achieve this ?
Regards
Umair
cURL, you seek cURL! (Seriously, cURL is awesome. Scroll down at that link and you'll find the HTTP Authentication example below)
// HTTP authentication
$url = "https://www.example.com/protected/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //see link below
$result = curl_exec($ch);
curl_close($ch);
echo $result;
You can find information about using CURLOPT_SSL_VERIFYPEER here.

Categories