douba8048 2019-04-12 10:13
浏览 84

使用xml架构解析XML响应

Edit The code given below will work in localhost as it is, if anyone want to copy and try it.The given credentials are valid.

I have a php code which requests data from an API service. An XML request is sent and in response XML data is recieved. I have stored the respose data in a variable $output. When doing echo $output, the details in the XML response is printed. But now I need to parse this response and store the required data in variables. E.g: I need to save $customer_id = value from the <customer_id>12345</customer_id>. I did a thorough google search and tried all the snippets provided by different developers, but no use.

I tried var_dump(simplexml_load_string($output)); and it is returning object(SimpleXMLElement)#1 (0) { }. I even tried converting the XML data to array.

index.php

    <?php

        $appId ="MFS149250";
        $appPass ="5TEBRPCZ";
        $brokeCode ="ARN-149250";
        $iin = "5011217983";


    $xml_data = '<?xml version="1.0" encoding="UTF-8"?>
                <NMFIIService>
                    <service_request>
                        <appln_id>'.$appId.'</appln_id>
                        <password>'.$appPass.'</password>
                        <broker_code>'.$brokeCode.'</broker_code>
                        <iin>'.$iin.'</iin>
                    </service_request>
                </NMFIIService>';



      $URL = "https://uat.nsenmf.com/NMFIITrxnService/NMFTrxnService/IINDETAILS";
                $ch = curl_init($URL);
                curl_setopt($ch, CURLOPT_MUTE, 1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
                curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $output = curl_exec($ch);
                //echo "<textarea>".$output."</textarea>";
                echo $output;
                var_dump(simplexml_load_string($output));
                curl_close($ch);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据