我正在尝试提取 Soap 响应的 HTTP 状态代码。 例如我有:
$client = new SoapClient($wsdl);
$client->__setSoapHeaders(
new SoapHeader(
$nameSpace,
'Security',
$secHeaderValue,
true
)
);
// The actual call
$response = $client->Complete($paramswebservice)
现在我通过这种方式获取响应 header :
$responseHeaders = $client->__getLastResponseHeaders();
var_dump($responseHeaders);
这是 vardump 的结果:以这种方式格式化的字符串(网络浏览器 - 页面源代码)
我现在正在做什么来提取http状态代码“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];
}
我真的不喜欢这个解决方案。我想要一个更强大/一致的。有什么建议吗?
编辑 1
正如评论中所问:
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
$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);
输出:
Array
(
[0] => 200
)
关于PHP:来自 HTTP header 的 SoapClient 响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112655/