【问题标题】:CakePHP 3.5: How to convert xml reponse to app response using phpCakePHP 3.5:如何使用 php 将 xml 响应转换为应用程序响应
【发布时间】:2020-02-05 23:55:13
【问题描述】:

我正在使用 CakePHP 框架并使用 HttpClient(cakephp) 获取响应。我尝试将 xml 转换为 php,但它在使用此函数 SimpleXMLElement() 时返回空对象

<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:body>
    <getaccountbalanceresponse xmlns="http://PegPayTelecomsApi/">
        <getaccountbalanceresult>
            <balance>xxx</balance>
            <statusdesc>xxxx</statusdesc>
            <statuscode>xxxx</statuscode>
        </getaccountbalanceresult>
    </getaccountbalanceresponse>
</soap:body>

如何将 xml 转换为 php。任何人都可以帮助我..

我的php代码是

$this->client = new Client();
    $this->response->statusCode(200);

    $this->RequestHeader = [
        'headers' => [
            'cache-control' => 'no-cache',
            'content-type' => 'text/xml',
        ],
        'ssl_verify_peer' => false,
        'ssl_verify_host' => false,
        'ssl_verify_peer_name' => false,
    ];

    $this->OAuthUrl = 'https://test.pegasus.co.ug:8019/pegpaytelecomsapi/PegPayTelecomsApi.asmx';


    $this->RequestAccessBody = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
    <GetAccountBalance xmlns="http://PegPayTelecomsApi/">
        <vendorcode>CCCCC</vendorcode>
        <password>RRRRRRR</password>
    </GetAccountBalance>
</Body>

';

    $response = $this->client->post($this->OAuthUrl,$this->RequestAccessBody,$this->RequestHeader);

    $soap    = simplexml_load_string($response->body);

【问题讨论】:

  • xml 到 php ??? CakePHP 版本???添加您的 PHP 代码。
  • 是的兄弟..版本是3.5
  • 不要在评论中添加代码,更新你的问题!
  • 并阅读如何将 XML 转换为数组 book.cakephp.org/3/en/core-libraries/…
  • 能否为问题添加一些示例代码

标签: php xml cakephp xml-parsing xmlhttprequest


【解决方案1】:
$response = $this->client->post($this->OAuthUrl,$this->RequestAccessBody,$this->RequestHeader);

$soap = $response->xml; // xml response as array

debug($soap);

阅读:

https://book.cakephp.org/3/en/core-libraries/httpclient.html#reading-json-and-xml-response-bodies

【讨论】:

  • 感谢你们的cmets。但它不起作用,$reponse->xml 返回空值。但我打印 $response->body 给出一些 xml 代码,以便代码如何更改为 php 中的数组。请帮助我。我提到了我有问题的输出。请检查一下。
猜你喜欢
  • 2018-06-02
  • 2015-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
相关资源
最近更新 更多