views:

523

answers:

5

I'm trying to use file_get_contents() to get the response from a server and this error was encountered. Could someone tell me what is the reason and how to fix it? The portion of the code is:

$api = "http://smpp5.routesms.com:8080/bulksms/sendsms?username=$username&password=$password&source=$source&destination=$destin&dlr=$dlr&type=$type&message=$message";
$resp = file_get_contents($api);

The server responded correctly while I pasted the url in the browser. I learned that this is caused by the server rejecting the client's HTTP version, but I have no idea why that is happening in my case.

Any help is much appreciated. Thanks in advance

A: 

Can you sniff what's happening on the wire? Seeing the format of the HTTP request as it goes out on the wire would help a lot.

Without seeing that, my best guess would be that the server isn't well-implemented, and is rejecting a HTTP/1.1 request. Try setting --http1.0 on Curl and seeing what happens...

Mark Nottingham
could you please tell me how to see the format of the HTTP request..?
shyam
You can't do so with file_get_contents. It's an extremely braindead fetch method that gives you no control over how the HTTP request is made. No headers are returned, and you cannot do anything but a simple 'GET'. To get headers and/or do other HTTP request methods, you'll have to use CURL
Marc B
+3  A: 

You could create a stream context with the HTTP version set to 1.0 and use that context with file_get_contents:

$options = array(
    'http' => array(
        'protocol_version' => '1.0',
        'method' => 'GET'
    )
);
$context = stream_context_create($options);
$api = "http://smpp5.routesms.com:8080/bulksms/sendsms?username=$username&password=$password&source=$source&destination=$destin&dlr=$dlr&type=$type&message=$message";
$resp = file_get_contents($api, false, $context);

By the way: Don’t forget to escape your URI argument values properly with urlencode.

Gumbo
+1  A: 

I found the problem, and it was in the coding. My bad.
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.
But thanks for all of your time and responses, those were informational.

shyam
A: 

Hi Shyam,

Would you mind sharing your solution. I have a routesms account too and I am writing an application and need help.

Thanks

Cyberned
if you're getting a 505 (version not supported), it's 'coz the url parameters aren't urlencoded. There might be a space in a parameter. In my case I think I'd missed to urlencode the message. Check the api call to verify. Tell me if it doesn't work
shyam
I get the error cannot open stream. I have tried urlencoding from reading your experience but didnt help. Just like in your case works perfectly when I paste into browser but thats how far I have gone. All other part of my code works just the routesms api bit left.
Cyberned
In that case ensure that allow_url_fopen is enabled in php.ini. Make sure that you can call urls to domain's outside yours.
shyam
A: 

I get the error cannot open stream. I have tried urlencoding from reading your experience but didnt help. Just like in your case works perfectly when I paste into browser but thats how far I have gone. All other part of my code works just the routesms api bit left.

Cyberned