Asked  7 Months ago    Answers:  5   Viewed   67 times

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 = "$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



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.

Wednesday, March 31, 2021
answered 7 Months ago

Replace the space in the URL with its percent-encoding:

    $page = file_get_contents("$i");
Saturday, May 29, 2021
answered 5 Months ago

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, May 29, 2021
answered 5 Months ago

The content option is used with POST and PUT requests. For GET you can just append it as a query string:

file_get_contents(''.$getdata, false, $context);

Furthermore, the method defaults to GET so you don't even need to set options, nor create a stream context. So, for this particular situation, you could simply call file_get_contents with the first parameter if you wish.

Friday, July 23, 2021
answered 3 Months ago

I think I have it with:

    catch (WebException ex)

        if (ex.Status == WebExceptionStatus.ProtocolError)
            int statusCode = (int) ((HttpWebResponse) ex.Response).StatusCode;
            listenerContext.Response.StatusCode = statusCode;
            listenerContext.Response.StatusDescription = ex.Message;
            log("WARNING", uri, "WebException/ProtocolError: " + ex.GetType() + " - " + ex.Message);
            log("ERROR", uri, "WebException - " + ex.GetType() + " - " + ex.Message);


Wednesday, August 25, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :