3

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 
1
  • Can you please provide print_r($response) instead of Image? Commented Mar 30, 2017 at 8:50

1 Answer 1

5

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
)
Sign up to request clarification or add additional context in comments.

3 Comments

Your solution is better. But i'm still wondering, there's no SoapClient methods or any other way to get that http code ? With something close to getResponseHttpCode() or such ?
I have researched for it. But __getLastResponseHeaders returns string, for which i think preg_match can be good solution
You link is broken. Could you add code directly into the answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.