gpt4 book ai didi

php - 解析多部分 SOAP 附件的正确方法?

转载 作者:行者123 更新时间:2023-12-04 18:10:23 24 4
gpt4 key购买 nike

直接使用 SoapClient 我得到一个异常:“看起来我们没有 XML 文档”,但是通过捕获异常并访问 $client->__lastReponse() 我得到了完整的消息 -此处描述:http://www.w3.org/TR/SOAP-attachments#SOAPMultipart

例子:

------=_Part_13_5075710.1359624351743
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Id: <740ABC2FC7835A4DF5526C699A3C302D>

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:GetOneFileResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://schemas.cisco.com/ast/soap/"><DataHandler href="cid:8D6C4A5AD0F2C0339324FBFC92417B5C" xsi:type="ns2:DataHandler" xmlns:ns2="DimeGetFileService"/></ns1:GetOneFileResponse></soapenv:Body></soapenv:Envelope>
------=_Part_13_5075710.1359624351743
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: <8D6C4A5AD0F2C0339324FBFC92417B5C>

Binary data here

------=_Part_13_5075710.1359624351743--

假设二进制数据是我尝试通过 SOAP 服务下载的示例请求(作为文本),正确的解析方法是什么。

我想在没有任何额外扩展的情况下解决这个问题,例如 WSO2似乎具有二进制附件(MTOM)处理能力,但需要扩展

最佳答案

这绝对不是执行此操作的“正确方法”,但它适用于我的用例:

$client = new SoapClient($wsdl);
try {
$client->MySoapFunction($argument);
} catch (SoapFault $e)
{
if ($e->getMessage() == 'looks like we got no XML document') {
$response = $client->__getLastResponse();

$data = $this->_stripSoapHeaders($response);
return $this->_parseMimeData($data);
}

protected function _stripSoapHeaders($response)
{
// Find first occurance of xml tag
preg_match('/(?<xml><.*?\?xml version=.*>)/', $response, $match);
$xml = $match['xml'];

// Strip SOAP http headers, and SOAP XML
$offset = strpos($response, $xml) + strlen($xml . PHP_EOL);
return substr($response, $offset);
}
protected function _parseMimeData($data)
{
// Find MIME boundary string
preg_match('/--(?<MIME_boundary>.+?)\s/', $data, $match);
$mimeBoundary = $match['MIME_boundary']; // Always unique compared to content

// Copy headers to client
if (preg_match('/(Content-Type: .+?)'.PHP_EOL.'/', $data, $match)) {
header($match[1]);
}
$contentType = $match[1];
if (preg_match('/(Content-Transfer-Encoding: .+?)'.PHP_EOL.'/', $data, $match)) {
header($match[1]);
}

// Remove string headers and MIME boundaries from data
preg_match('/(.*Content-Id.+'.PHP_EOL.')/', $data, $match);
$start = strpos($data, $match[1]) + strlen($match[1]);
$end = strpos($data, "--$mimeBoundary--");
$data = substr($data, $start, $end-$start);

return trim($data, "\r\n");
}

使用上面的代码,我通过 SOAP API 成功下载了 gzip 二进制文件。

关于php - 解析多部分 SOAP 附件的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14622830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com