【问题标题】:How to Change Magento2 SOAP API response from Object to XML如何将 Magento2 SOAP API 响应从对象更改为 XML
【发布时间】:2017-07-27 08:11:06
【问题描述】:

我创建了一个自定义 Magento2 Soap API 来从第三方服务器获取数据并将其保存到 Magento DB 中。 数据处理一切正常。现在,当我打印结果时,它是对象形式,但我只需要 XML 格式。 这是我用来发出请求的代码:

 $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';
    }

这个 print_r($soapResponse) 显示结果如下:

stdClass 对象([result] => 成功)

我只需要 XML 格式的结果。

如果有人已经在这方面工作,请告诉我。

【问题讨论】:

  • 您是否尝试过wddx_serialize_value 函数将您的对象转换为xml?像这样echo wddx_serialize_value($soapResponse);
  • 感谢您的回复,Shahroze!这样,我们只能更改客户端站点的数据,即从我们向 Magento SOAP API 发出请求的位置。但是我要在 Magento API 端更改此响应,因此请求 API 的用户不需要在其端执行此操作。
  • @ManjuCh 请问你是怎么解决这个问题的?
  • @Gem:实际上并没有解决,我们只是修改了第三方端的代码,使用Object形式而不是XML。

标签: php api soap magento2


【解决方案1】:

试试下面的代码

$soapResponse = array_flip((array) $soapResponse);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($soapResponse, array ($xml, 'addChild'));
print $xml->asXML();

你会得到如下结果

<root><result>Success</result></root>

来源How to convert array to SimpleXML

【讨论】:

  • 您好,感谢您的回复。实际上,我想在服务器端更改它,而不是在客户端。即 $soapResponse = $soapClient ->CustomDataApiManagementV1ProcessData($addArgs); $soapResponse 应该是 XML 格式。
  • xml 格式的 $soapResponse 有什么具体原因吗?我不确定 Response 是否会得到 xml 格式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 2013-06-01
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
相关资源
最近更新 更多