Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: SoapClient Response code from HTTP headers

I'm trying to extract the Soap response's HTTP status code. So for instance i have :

$client = new SoapClient($wsdl);
$client->__setSoapHeaders(
    new SoapHeader(
        $nameSpace,
        'Security',
        $secHeaderValue,
        true
    )
);

// The actual call
$response = $client->Complete($paramswebservice)

So now i'm getting the response headers, this way :

$responseHeaders = $client->__getLastResponseHeaders();
var_dump($responseHeaders);

This is the result of the vardump : a string formatted this way (web browser - page source code)

enter image description here

What i am doing right now to extract the http status code '200' :

/**
 * Returns the HTTP Status code of $response
 * @param string $response
 * @return string
 */
function extract_response_http_code($response) {
    $tmp = explode('\n', $response);
    $array = explode(' ', $tmp[0]);

    return $array[1];
}

I really don't like this solution. I would like a more robust / consistent one. Any suggestions ?

EDIT 1

As asked in the comments :

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Length: 1315
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Thu, 30 Mar 2017 08:52:15 GMT 
like image 804
Elbarto Avatar asked Aug 07 '26 14:08

Elbarto


1 Answers

PHP code demo

<?php
$result="HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Length: 1315
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Thu, 30 Mar 2017 08:52:15 GMT";
preg_match("/HTTP\/\d\.\d\s*\K[\d]+/", $result,$matches);
print_r($matches);

Output:

Array
(
    [0] => 200
)
like image 189
Sahil Gulati Avatar answered Aug 09 '26 03:08

Sahil Gulati



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!