Viewed   219 times

I am trying to use file_get_contents.I have made sure that allow_url_fopen is enabled in php.ini. As of now it is telling me:

[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

All I'm doing is the following, which I can access through browser without a problem.

$url=('http://site/@api/users/=john_s[email protected]/properties');

I believe this is an authentication issue but not sure how I can supply the proper credentials from within the script itself. Any ideas would be greatly appreciated.



In your url try:

http://user:[email protected]/ 

(append whatever the rest of the URL for your API should be)

Friday, November 4, 2022

I found the problem, and it was a simple coding error -- missing url encoding.

The reason I didn't notice it at first was because the code was ok before I did some editing, and I'd missed out the urlencode() function before calling the server, which caused a space in the url.

This does seem to be the reason this error occurs for most people. So if you encounter this, use urlencode() on all variables which may contain white space in it's value used as URL parameters. So in the case in my question the fixed code will look like:

$api = "$username&password=$password&source=$source&destination=$destin&dlr=$dlr&type=$type&message=" . urlencode($message);
$resp = file_get_contents($api);

Also, thanks for all of your time and responses, those were informational.

Monday, November 21, 2022

Try it like this. Might be your formatting. This works for me.

$ytrequrl = ''.$apikey_YT;

$info= file_get_contents($ytrequrl);
$info= json_decode($info, true);

Friday, November 4, 2022

Fixed it. In case anyone has the same problem:

$url = "http://some-adress/test.php";
$headers = get_headers($url, 1);
$content_length = $headers["Content-Length"];
$content = file_get_contents($url, NULL, NULL, NULL, $content_length);
echo $content;

Saturday, December 3, 2022

Try using cURL.


curl_setopt($curl_handle, CURLOPT_URL,'http://###.##.##.##/mp/get?mpsrc= format=flv');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);

Monday, December 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :