gpt4 book ai didi

PHP - 扩展 SoapClient 以处理 SWA(带附件的 SOAP)

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:39:30 25 4
gpt4 key购买 nike

我正在尝试处理来自 Java SOAP 服务的 SWA 响应。在该 SWA 响应中,二进制附件连同一些 MIME header 附加到 XML 的末尾。我不能将 WSO2 用于依赖性要求限制。

如有任何帮助,我们将不胜感激!

// Input

------=_Part_42_539586119.1332526191981
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Id: <03B4708A9544C182C43E51D9ADA1E456>

<?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> ... TRUNCATED XML SOAP RESPONSE ... </soapenv:Body></soapenv:Envelope>

------=_Part_42_539586119.1332526191981
Content-Type: image/png
Content-Transfer-Encoding: binary
Content-Id: <D637B1257E3E5EEA06AF0E45494F8448>

BINARY DATA GOES HERE


// End of input

//拆分响应的脚本 + 像父类 SoapClient 一样返回 StdObj + 对文件做一些事情

namespace Project;
class SoapClient extends \SoapClient
{
public function __call ($function_name, $arguments)
{
// have the parent do a soap call, catch the lastResponse() if an error
// occurred (eg has an attachment) and parse it out.
try {
$r = parent::__call($function_name, $arguments);
return $r;
} catch (\Exception $e) {
// Assumption: When this is sent, it means that a file is being sent
// because SimpleXML can't process it.
if ($e->getMessage() == "looks like we got no XML document") {
$response = parent::__getLastResponse();
$partString = "/(------=_[a-zA-Z0-9_\\.]+)/";
$outputArr = preg_split($partString, $response);
// $outputAtt[0] -- empty and is the first MIME Part Header
// $outputArr[1] -- Mime Header + XML (The SOAP Response)
// $outputArr[n+1] -- additional files w/ MIME headers
if (array_key_exists(1, $outputArr)) {
// remove the first 5 lines (4 MIME Header lines) + 1 Blank
// line
$data = implode("\n",
array_slice(explode("\n", $outputArr[1]), 5));

/// Simple XML Object ... appears to be an empty SimpleXMLElement though ... >:-(
$xml = simplexml_load_string($data, null, null, "http://schemas.xmlsoap.org/soap/envelope/");




} else {
// OK Maybe this doesn't actually contain the XML... throw
// the original exception.
throw new \SoapFault($e->getMessage(), $e->getCode(),
$e->getPrevious());
}
} else {
throw new \SoapFault($e->getMessage(), $e->getCode(),
$e->getPrevious());
}
}
}
}

最佳答案

使用 MIME parser为了这。从那里开始非常简单:

require_once('MimeMailParser.class.php');

class SwADownloadSoapClient extends SoapClient
{
const ATTACHMENT_DIR = '/path/to/saved/attachments/';

public function __doRequest(
$request, $location, $action, $version, $one_way=0
) {
// Issue the SOAP request as SoapClient would normall
$sResult = parent::__doRequest(
$request, $location, $action, $version, $one_way);

// Handle and parse MIME-encoded messages
// @note We're not doing much inspection
// of the XML payload against the attachments ATM
// so not sure how greatly this lives up to the spec
$sResult = $this->_parseMimeMessage($sResult);

$oParser = new MimeMailParser();
$oParser->setText($sResult);

// Save the attachments
$aAttachments = $oParser->getAttachments();
foreach($aAttachments as $oAttachment) {
$sFile = $oAttachment->filename;
if($rFp = fopen(self::ATTACHMENT_DIR . $sFile, 'w')) {
while($sBytes = $attachment->read())
fwrite($rFp, $sBytes);
fclose($rFp);
}
}
}
}

如果你想更恰本地实现the spec ,您需要将 MIME 附件与 SOAP XML 进行匹配。

关于PHP - 扩展 SoapClient 以处理 SWA(带附件的 SOAP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9844470/

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