如何在php中解析soap响应

时间:2013-11-20 12:14:49

标签: php xml parsing soap xml-parsing

我需要帮助解析PHP中的XML响应,这是通过调用Web服务获得的。解析器的类型并不重要(DOM / SAX / ETC ..),但请注意,XML文件不长。 (最多约50行)。我尝试使用simplexml_load_string但我无法从中提取数据。我认为与肥皂或信封名称空间有关。

回应:

<?xml version="1.0" encoding="utf-8"?>
<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>
<ProcessRequestResponse xmlns="http://xxxxxxxxx.yyy">
<ProcessRequestResult>
<OutputData>

&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;DATA&gt;
&lt;RECORD ID="1"&gt;&lt;COURSE_ID&gt;44226&lt;/COURSE_ID&gt;&lt;COURSE_NUMBER&gt;1234567-0&lt;/COURSE_NUMBER&gt;&lt;/RECORD&gt;

&lt;RECORD ID="2"&gt;&lt;COURSE_ID&gt;44227&lt;/COURSE_ID&gt;&lt;COURSE_NUMBER&gt;1234598-0&lt;/COURSE_NUMBER&gt;&lt;/RECORD&gt;

&lt;/DATA&gt;
</OutputData>
<ErrorMessage />
<Result>Success</Result>
</ProcessRequestResult>
</ProcessRequestResponse>
</soap:Body>
</soap:Envelope>

这是我从服务器获取的 EXACT 响应。

请帮我解析这个xml!我此刻真的迷失了。

编辑:修复了soap正文,以便记录结束标记。

3 个答案:

答案 0 :(得分:0)

尝试通过定义SoapClient classmap option来调用SOAP Web服务。此外,您必须通过调用html_entity_decode然后simplexml_load_string来加载OutputData。

你也可以尝试使用WSDL到php生成器,它可以让你轻松调用和接收响应,例如https://github.com/mikaelcom/WsdlToPhp

答案 1 :(得分:0)

我是怎么做的,创造eway soap请求并获得eby soap结果,可以帮助别人!

<?php
    $URL = "https://www.eway.com.au/gateway/rebill/test/manageRebill_test.asmx?wsdl";
                $option = array("trace"=>true);
                $client = new SOAPClient($URL, $option);
                $functions = $client->__getFunctions();
        $headeroptions=array('eWAYCustomerID'=>"87654321",'Username'=>"test@eway.com.au","Password"=>"test123");
                $header = new SOAPHeader('http://www.eway.com.au/gateway/rebill/manageRebill', 'eWAYHeader',$headeroptions);

                $bodyoptions = array(
                                "CreateRebillCustomer" => array(
                                    "customerTitle" => "Mr",                   
                                      "customerFirstName"=>"Muhammad",
                                      "customerLastName"=>"Shahzad",
                                      "customerAddress"=>"cust ome rAddress",
                                      "customerSuburb"=>"customer Suburb",
                                      "customerState"=>"ACT",
                                      "customerCompany"=>"customer Company",
                                      "customerPostCode"=>"2345",
                                      "customerCountry"=>">Australia",
                                      "customerEmail"=>"test@gamil.com",
                                      "customerFax"=>"0298989898",
                                      "customerPhone1"=>"0297979797",
                                      "customerPhone2"=>"0297979797",
                                      "customerRef"=>"Ref123",
                                      "customerJobDesc"=>"customerJobDesc",
                                      "customerComments"=>"customerComments",                    
                                      "customerURL" => "http://www.acme.com.au"
                                   )
                                );

                    try{
                        $response = $client->__soapCall("CreateRebillCustomer", $bodyoptions,NULL,$header,$outputHeader);
                        //echo $client->__getLastRequest();
                        //$response = $client->CreateRebillCustomer($bodyoptions);
                        //echo "<pre>";echo "<br/>";
                       // print_r($response);        
                            echo    $result         = $response->CreateRebillCustomerResult->Result;echo "<br/>";
                            echo    $customerId     = $response->CreateRebillCustomerResult->RebillCustomerID;echo "<br/>";
                            echo "<br/>";
                        if($result=='Success' AND $customerId){
                            echo 'Member Created at eWay Successfully!...<br/>';
                            echo 'Creating Recurring Billing Cycle on eWay,Please wait......<br/>';
                            //$UpdateRebillCustomer = CreateRebillEvent($customerId);
                            //print_r($UpdateRebillCustomer);

                        }
                        else{
                            echo    $ErrorSeverity  = $response->CreateRebillCustomerResult->ErrorSeverity;echo "<br/>";
                            echo    $ErrorDetails   = $response->CreateRebillCustomerResult->ErrorDetails;echo "<br/>";
                        }
                    }
                    catch(SOAPFault $e){
                        print $e;
                    }

                    if($customerId){


                                $bodyoptions2 = array(
                                "CreateRebillEvent " => array(
                                      "RebillCustomerID" => $customerId,                   
                                      "RebillInvRef" => "Ref123",
                                      "RebillInvDes"=>"description",
                                      "RebillCCName"=>"Mr Andy Person",
                                      "RebillCCNumber"=>"4444333322221111",
                                      "RebillCCExpMonth"=>"12",
                                      "RebillCCExpYear"=>"15",
                                      "RebillInitAmt"=>"100",
                                      "RebillInitDate"=>date('d/m/Y'),
                                      "RebillRecurAmt"=>"200",
                                      "RebillStartDate"=>date('d/m/Y'),
                                      "RebillInterval"=>"31",
                                      "RebillIntervalType"=>"1",
                                      "RebillEndDate"=>"31/12/2013",                     
                                   )
                                );
                        try{    
                            $response = $client->__soapCall("CreateRebillEvent", $bodyoptions2,NULL,$header,$outputHeader);
                            //echo   $client->__getLastRequest();           
                            //print_r($response);
                            echo "<br/>";
                              echo  $result2        = $response->CreateRebillEventResult->Result;echo "<br/>";
                              echo  $RebillCustomerID   = $response->CreateRebillEventResult->RebillCustomerID;echo "<br/>";
                            if($result2=='Success'){
                                echo 'Recurring Cycle Created Successfully at eWay!...<br/>';
                                echo 'Member Id is ===>'.$RebillCustomerID;
                                //$UpdateRebillCustomer = CreateRebillEvent($customerId);
                                //print_r($UpdateRebillCustomer);                           
                            }
                            else{
                                echo    $ErrorSeverity  = $response->CreateRebillEventResult->ErrorSeverity;echo "<br/>";
                                echo    $ErrorDetails   = $response->CreateRebillEventResult->ErrorDetails;echo "<br/>";
                            }

                        }
                     catch(SOAPFault $e){
                        print $e;
                     }

                     }

                  ?>

答案 2 :(得分:0)

$xml = '<?xml version="1.0" encoding="utf-8"?>
                <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>
                    <ProcessRequestResponse xmlns="http://xxxxxxxxx.yyy">
                        <ProcessRequestResult>
                            <OutputData>123</OutputData>
                            <ErrorMessage />
                            <Result>Success</Result>
                        </ProcessRequestResult>
                    </ProcessRequestResponse>
                    </soap:Body>
                </soap:Envelope>';
        $doc = new DOMDocument();
        $doc->loadXML($xml);
        echo $doc->getElementsByTagName('OutputData')->item(0)->nodeValue;

结果是:

3