15

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

shyam
  • 6,681
  • 7
  • 28
  • 45
  • I am receiving the same error while using 'curl' too. – shyam May 03 '10 at 10:38
  • Try replacing your '$' with '%24'. It's probably not what's causing your issue, but you should properly urlencode your characters. – Kylar May 04 '10 at 16:34
  • the `$` is actually for the variable. And I've found what caused the error, and it's urlencoding. I didn't notice it earlier bcos the script was working earlier, and I'd missed the function after a bit of editing. Thanks to all the folks who tried to help...and indeed your responses where informational. – shyam May 05 '10 at 05:01
  • Can you post the working code properly? – LIGHT Aug 22 '12 at 16:03
  • @Prakash Check my own answer. – shyam Aug 24 '12 at 11:25
  • @shyam I still got some error, but found the solution. check my answer – LIGHT Aug 28 '12 at 15:50

6 Answers6

35

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 = "http://smpp5.routesms.com:8080/bulksms/sendsms?username=$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.

shyam
  • 6,681
  • 7
  • 28
  • 45
10

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
  • 643,351
  • 109
  • 780
  • 844
1

I ran into the same issue and in my case the culprit was an errant newline/CRLF character at the end of the request URL, which does not get caught by urlencode() (or maybe it does encode it but it still causes the server to produce the error). Once I found the problem the requests began to work again, even without the stream context options.

Hopefully this will help others.

therealklanni
  • 652
  • 1
  • 5
  • 15
0

Some time we still get error with

file_get_contents($api);

in that case, try this:

fopen($api,"r");
LIGHT
  • 5,604
  • 10
  • 35
  • 78
0

I was also facing this same issue.. later i found that while retrieving the results from mysql, Limit $count , $count was -ve. fixing that the url worked fine. There is some problem in url only, and its not a file_get_contents or http version issue..

kanchan
  • 339
  • 1
  • 3
  • 15
0

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
  • 5,546
  • 1
  • 25
  • 21
  • could you please tell me how to see the format of the HTTP request..? – shyam May 03 '10 at 10:59
  • 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 May 03 '10 at 12:51