views:

3100

answers:

5

Help,

I am having problems calling a url from PHP code. I need to call a service using a querystring 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?

Thanks!

+1  A: 

Hi!

I'm not sure about the parameters(mpaction, format), if they are specified for the amazonaws page or ##.##.

Try to urlencode() the url.

alexn
thanks - these are params for the mediaplug instance. If I urlencode the url it still does not work - I get a very garbled url in the error...??
undefined
what kind of error? urlencode doesn't garble url.
SilentGhost
You should be encoding only the parameter string: "convert format" should be "convert%20format" (or alternatively "convert+format").
bobince
+4  A: 

Could this be your problem?

Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

SilentGhost
I had the same error the OP had, and this was my problem - spaces in the arguments. `urlencode()` on the GET parameters solved the issue.
Walt W
A: 

I notice that your URL has spaces in it. I think that usually is a bad thing. Try encoding the URL with $my_url = urlencode("my url"); and then calling file-get-contents($my_url); and see if you have better luck.

shady
A: 

file_get_contents() utilizes the fopen() wrappers, therefore it is restricted from accessing URLs through the allow_url_fopen option within php.ini.

You will either need to alter your php.ini to turn this option on or use an alternative method, namely cURL - by far the most popular and, to be honest, standard way to accomplish what you are trying to do.

Michael Wales
Nevermind, just noticed he said `file_get_contents()` worked on a different URL. Nonetheless, this is still a good tip for others having this issue.
Michael Wales
yes, Ive looked at other options and see that cURL is the standard way to go but I tried this as it was easier and worked with other urls, I think i have to restart apache to intsall cURL? and cant figure out how to do this (another question)... thanks for your quick response
undefined
+3  A: 

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&amp;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);

?>
James Hall
undefined
Whoops, yeah, sorry about that typo. Did it work after changing that?
James Hall
Hi yeah - I got cURL to work so thaks very much... problem solved!
undefined