SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing SOAP respose

    I have been trying to parse the below soap response without any joy. I have tried going down the xpath way and now using simplexml_load_string without any joy.

    here is my soap response:
    PHP Code:
    <envelope:Envelope xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/"> <envelope:Header></envelope:Header> <envelope:Body xmlns="SilverpopApi:EngageService.SessionMgmt.Login"> <RESULT xmlns="SilverpopApi:EngageService.SessionMgmt.Login" xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/"> <SUCCESS>true</SUCCESS> <SESSIONID>F9BA7881819F391EE478ACF9328F6EB5</SESSIONID> <ORGANIZATION_ID>134be9-12c81c26df6-4f4749e15ce6d7a21b02ab08b9b7921c</ORGANIZATION_ID> <SESSION_ENCODING>;jsessionid=F9BA7881819F391EE478ACF9328F6EB5</SESSION_ENCODING> </RESULT> </envelope:Body> </envelope:Envelope
    but my below code does nothing:
    PHP Code:
    $soaoresponce $client->__getLastResponse();
    //echo $soaoresponce->SESSIONID;
    //echo htmlentities($soaoresponce);

    $strSessionID simplexml_load_string($soaoresponce);
     foreach(
    $strSessionID as $key){
                     
    //echo $key;
                     
    echo $key->RESULT[1]->SUCCESS;
                 } 
    can anyone correct me.

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Almost certain you doing it wrong. The way to parse SOAP response is to use SoapClient class in php. It will take care of all the parsing for you and you get back an object of stdClass

    How are you getting the raw xml from the soap service anyway?

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using the SoapClient class.

    this is my code:

    PHP Code:
    public function loginRequest(){
            
                    
        
    $client = new SoapClient('http://wsdl', array("trace"=> 1'cache_wsdl' => WSDL_CACHE_NONE'soap_version'=>SOAP_1_1));
        
        
    $error 0;
        
        try {
                
              
    $login $client->Login(array('USERNAME'=>$this->username'PASSWORD'=>$this->password));
             
                        
                echo 
    "RESPONSE:\n";
                
                
    $soaoresponce $client->__getLastResponse();
    //echo $soaoresponce->SESSIONID;
    //echo htmlentities($soaoresponce);

    $strSessionID simplexml_load_string($soaoresponce);
     foreach(
    $strSessionID as $key){
                     
    //echo $key;
                     
    echo $key->RESULT[1]->SUCCESS;
                 }  

                   
        } catch (
    SoapFault $fault) {
             
                
    $error 1;
                print(
    '<pre>');
                echo 
    "GENERAL EXCEPTION:<br />".$fault->faultcode."-".$fault->faultstring."\n<br />";
                
                echo 
    "EXCEPTION:\n";
                
    print_r($fault);
                
                echo 
    "REQUEST:\n";
                echo 
    htmlentities($client->__getLastRequest());
                echo 
    "<br />";
                
                echo 
    "REQUEST HEADERS:\n";
                echo 
    $client->__getLastRequestHeaders();
                
                
                echo 
    "RESPONSE HEADERS:\n";
                echo 
    $client->__getLastResponseHeaders();
                
                echo 
    "RESPONSE:\n";
                echo 
    htmlentities($client->__getLastResponse());
                
                print(
    '</pre>');
            }
           
        } 

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK then the $login is your result object, you can work with it directly, most likely
    $login->RESULT will be your result.
    try
    var_dump($login);

    The $client->__getLastResponse(); is usually used for debugging only, not a way to parse xml response.

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doing this: $login->RESULT brings nothing back.

    doing a var_dump of $login brings this back:

    PHP Code:
    object(stdClass)#3 (4) { ["SUCCESS"]=> bool(true) ["SESSIONID"]=> string(32) "944354FDE258349D03C81119EA481580" ["ORGANIZATION_ID"]=> string(51) "134be9-12c81c26df6-4f4749e15ce6d7a21b02ab08b9b7921c" ["SESSION_ENCODING"]=> string(44) ";jsessionid=944354FDE258349D03C81119EA481580" } 
    My issue is how to say echo the value of a specific element. For example I want to output the value of SUCCESS.

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got it working using xpath.

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after further testing I was able to use $login->SUCCESS directly as well, rather than going down the xpath way. wasted so much time on this

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I was talking about. The whole point of using SoapClient is that it does all the parsing of xml for you and you get an object of stdClass to work with, just access the properties of that object directly.

    That's the advantage of Soap over Rest API - very easy to use from the client side.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •