【问题标题】:PHP: SoapClient Response code from HTTP headersPHP:来自 HTTP 标头的 SoapClient 响应代码
【发布时间】:2017-03-30 08:40:57
【问题描述】:

我正在尝试提取 Soap 响应的 HTTP 状态代码。 所以例如我有:

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

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

所以现在我通过这种方式获取响应标头:

$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

如 cmets 中所问:

HTTP/1.1 200 正常 缓存控制:私有,max-age=0 内容长度:1315 内容类型:文本/xml;字符集=utf-8 服务器:Microsoft-IIS/8.5 X-Powered-By: ASP.NET 日期:2017 年 3 月 30 日星期四 08:52:15 GMT

【问题讨论】:

  • 你能提供print_r($response)而不是图片吗?

标签: php http soap soap-client


【解决方案1】:

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
)

【讨论】:

  • 您的解决方案更好。但我仍然想知道,没有 SoapClient 方法或任何其他方式来获取该 http 代码吗?有接近 getResponseHttpCode() 之类的东西吗?
  • 我已经研究过了。但是__getLastResponseHeaders 返回字符串,我认为preg_match 可以是很好的解决方案
猜你喜欢
  • 2014-10-10
  • 2014-10-12
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
相关资源
最近更新 更多