如何在php中阅读soap response xml

时间:2013-12-05 06:35:58

标签: php soap

这是我的Soap Response xml,我需要RebillCustomerID 我怎么能这样做?

 <?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:Header><eWAYHeader 
   xmlns="http://www.eway.com.au/gateway/rebill/manageRebill">
    <eWAYCustomerID>87654321</eWAYCustomerID><Username>test@eway.com.au</Username>
      <Password>test123</Password></eWAYHeader></soap:Header><soap:Body>
        <CreateRebillCustomerResponse xmlns="http://www.eway.com.au/gateway/rebill/manageRebill"><CreateRebillCustomerResult>
           <Result>Success</Result><ErrorSeverity /><ErrorDetails />
      <RebillCustomerID>90246</RebillCustomerID></CreateRebillCustomerResult>                             
          </CreateRebillCustomerResponse></soap:Body></soap:Envelope><pre></pre>

3 个答案:

答案 0 :(得分:10)

尝试下面的代码。我假设您的xml响应位于$soapXMLResult变量

$soap     = simplexml_load_string($soapXMLResult);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->CreateRebillCustomerResponse;
$customerId = (string) $response->CreateRebillCustomerResult->RebillCustomerID;
echo $customerId;

答案 1 :(得分:1)

我是怎么做的,创造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)

如果您正在为SOAP服务编写客户端,并且希望通过将其放在本地文件中并对其进行解析来操作或测试它们的响应(可能它们的响应有错误并且您想自己修复错误),那么您可以通过直接调用__soapCall()覆盖soapClient用于调用方法的URL:

$result = $this->soap_client->__soapCall( 'function_call_name', ['location' => $file_location] );

其中$file_location是您的xml响应副本的绝对路径或相对路径。您放置的函数调用名称无关紧要,因为soap客户端将获得相同的xml响应,无论您尝试以何种方式调用此函数。唯一重要的是你的$file_location是正确的,并且如果你在浏览器中运行它,那么xml文件具有被apache读取的正确权限。

然后,您可以var_dump($response)找到所需的节点,而不是使用simplexml_load_string。如果你这样做,你可以在以后轻松换出你的xml。