PHP:来自 HTTP header 的 SoapClient 响应代码

标签 php http soap soap-client

我正在尝试提取 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 的结果:以这种方式格式化的字符串(网络浏览器 - 页面源代码)

enter image description here

我现在正在做什么来提取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 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);

输出:

Array
(
    [0] => 200
)

关于PHP:来自 HTTP header 的 SoapClient 响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43112655/

相关文章:

php - SSE 或长轮询共享主机上的实时通知?

http - 使用tomcat http服务共享tomcat日志

image - 通过 http 加载的图像的大小(宽度、高度)

xml - 使用 PL/SQL 解析 SOAP 响应

php - 使用 Amazon API 搜索 ISBN 时出现问题

使用 ex.getMessage() 打印时,androidHttpTransport.call(...,...) 抛出空异常

php - 将 POST 回显到 SQL 字符串中

php - 在搜索中包含可能的搜索名称的下拉列表

php - 使用自定义身份验证适配器时检查 CakePHP 上的登录用户信息

node.js http.get 在对远程站点发出 5 次请求后挂起