Php 如何将Magento2 SOAP API响应从对象更改为XML
我创建了一个定制的Magento2SOAPAPI,用于从第三方服务器获取数据并将其保存到MagentoDB中。 数据处理一切正常。现在,当我打印结果时,它是一个对象形式,但我只需要XML格式。 以下是我用于发出请求的代码:Php 如何将Magento2 SOAP API响应从对象更改为XML,php,api,soap,magento2,Php,Api,Soap,Magento2,我创建了一个定制的Magento2SOAPAPI,用于从第三方服务器获取数据并将其保存到MagentoDB中。 数据处理一切正常。现在,当我打印结果时,它是一个对象形式,但我只需要XML格式。 以下是我用于发出请求的代码: $wsdlurl = 'MagentoStore/soap?wsdl&services=CustomDataApiManagementV1'; $token = 'XXXXXXXXXXXX'; $opts = ['http' => ['header'
$wsdlurl = 'MagentoStore/soap?wsdl&services=CustomDataApiManagementV1';
$token = 'XXXXXXXXXXXX';
$opts = ['http' => ['header' => "Authorization: Bearer " . $token]];
$context = stream_context_create($opts);
$addArgs = array('xmldata'=> 'testData');
try{
$soapClient = new SoapClient($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$soapResponse = $soapClient
->CustomDataApiManagementV1ProcessData($addArgs);
print_r($soapResponse);
}catch(Exception $e){
echo 'error';
}
此打印($soapResponse)显示的结果如下:
stdClass对象([result]=>success)
我只需要XML格式的结果
请务必告诉我是否有人已经使用过它。请尝试下面的代码
$soapResponse = array_flip((array) $soapResponse);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($soapResponse, array ($xml, 'addChild'));
print $xml->asXML();
$soapResponse=array\u flip((array)$soapResponse);
$xml=新的SimpleXMLElement(“”);
array_walk_recursive($soapResponse,array($xml,'addChild'));
打印$xml->asXML();
您将得到如下结果
<root><result>Success</result></root>
成功
来源于请尝试以下代码
$soapResponse = array_flip((array) $soapResponse);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($soapResponse, array ($xml, 'addChild'));
print $xml->asXML();
$soapResponse=array\u flip((array)$soapResponse);
$xml=新的SimpleXMLElement(“”);
array_walk_recursive($soapResponse,array($xml,'addChild'));
打印$xml->asXML();
您将得到如下结果
<root><result>Success</result></root>
成功
Source from是否尝试使用
wddx\u serialize\u value
函数将对象转换为xml?像这样echo wddx\u序列化\u值($soapResponse)代码>感谢您的回复,Shahroze!通过这种方式,我们只能在客户机站点上更改数据,即从我们向Magento SOAP API发出请求的位置更改数据。但我想在Magento API端更改此响应,这样请求API的用户就不需要在他们的端执行此操作了。@ManjuCh我可以知道您是如何解决此问题的吗?@Gem:实际上它没有解决,我们刚刚修改了第三方端的代码,将其用于对象形式而不是XML。您是否尝试过使用wddx\u serialize\u value
函数将对象转换为XML?像这样echo wddx\u序列化\u值($soapResponse)代码>感谢您的回复,Shahroze!通过这种方式,我们只能在客户机站点上更改数据,即从我们向Magento SOAP API发出请求的位置更改数据。但是我想在Magento API端更改此响应,这样请求用户的API就不需要在他们端执行此操作了。@ManjuCh我可以知道您是如何解决此问题的吗?@Gem:实际上没有解决,我们只是修改了第三方端的代码,以对象形式而不是XML形式使用它。您好,谢谢您的响应。实际上,我想在服务器端更改它,而不是在客户端。i、 e.$soapResponse=$soapClient->CustomDataApiManagementV1ProcessData($addArgs)$soapResponse应为XML格式。您的$soapResponse采用XML格式有什么具体原因吗?。我不确定回复是否会得到xml格式。您好,谢谢您的回复。实际上,我想在服务器端更改它,而不是在客户端。i、 e.$soapResponse=$soapClient->CustomDataApiManagementV1ProcessData($addArgs)$soapResponse应为XML格式。您的$soapResponse采用XML格式有什么具体原因吗?。我不确定响应是否会得到xml格式。