Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php cURL returning all lowercase

Tags:

php

soap

curl

xml

I'm connecting to an API and need to retrieve XML data. I get the response, but the problem is that all keys are lowercase. If I test the request using SoapUI, it responds as expected so I've narrowed it down to my server and my code. This is my request:

$headers = array(
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: http://XYZDOMAIN.com.au/" . $func,
        "Content-length: " . strlen($xml_post_string),
        "Accept-Encoding: gzip,deflate"
    ); //SOAPAction: your op URL
    // PHP cURL  for https connection with auth

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_URL, $this->url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");

    $response = curl_exec($ch);

    if ($errno = curl_errno($ch)) {
        $error_message = curl_strerror($errno);
        echo "cURL error ({$errno}):\n {$error_message}";
    }

    $return = gzdecode($response);

    print_r($return);

The print_r at the end is there just for me to see what it returns, of course. The problem is that ALL of the keys returned in the XML are lowercase and it's messing up the rest of my code. This used to work just fine until a month ago and I have no idea what changed, but the only possible change was something on the server.

Can anybody point me in the right direction as to why the response is being changed to lowercase? Thank you!

like image 846
user1078494 Avatar asked Oct 19 '25 15:10

user1078494


1 Answers

The PHP version is using HTTP 2 instead of 1.1, and the HTTP 2 specification requires that headers are lower-cased. So that seems to be what's causing the difference.

https://httpwg.org/specs/rfc7540.html#HttpHeaders - "Just as in HTTP/1.x, header field names are strings of ASCII characters that are compared in a case-insensitive fashion. However, header field names MUST be converted to lowercase prior to their encoding in HTTP/2. A request or response containing uppercase header field names MUST be treated as malformed (Section 8.1.2.6)."

You may force http version to 1.1:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

to make sure you receive your headers in mixed case.

like image 162
Arthur Magamedov Avatar answered Oct 22 '25 04:10

Arthur Magamedov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!