Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file_get_contents() returns "failed to open stream: HTTP request failed!"

I am having problems calling a url from PHP code. I need to call a service using a query string from my PHP code. If I type the url into a browser, it works ok, but if I use file-get-contents() to make the call, I get:

Warning: file-get-contents(http://.... ) failed to open stream: HTTP request failed! HTTP/1.1 202 Accepted in ...

The code I am using is:

$query=file_get_contents('http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv'); echo($query); 

Like I said - call from the browser and it works fine. Any suggestions?

I have also tried with another url such as:

$query=file_get_contents('http://www.youtube.com/watch?v=XiFrfeJ8dKM'); 

This works fine... could it be that the url I need to call has a second http:// in it?

like image 812
undefined Avatar asked Mar 30 '09 14:03

undefined


1 Answers

Try using cURL.

<?php  $curl_handle=curl_init(); curl_setopt($curl_handle, CURLOPT_URL,'http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert 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); curl_close($curl_handle);  ?> 
like image 187
James Hall Avatar answered Sep 22 '22 02:09

James Hall