简体   繁体   中英

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)

在此处输入图片说明

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

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM