Results 1 to 8 of 8

Thread: [SOLVED] PHP SOAP XML Decode

  1. #1
    uxbod's Avatar
    uxbod is offline Moderator
    Join Date
    Nov 2006
    Location
    UK
    Posts
    8,017
    Rep Power
    24

    Default [SOLVED] PHP SOAP XML Decode

    Is there a easy way in PHP to decode the XML that comes back from a SOAP request into reference arrays ?
    Code:
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><context xmlns="urn:zimbra"><change token="12345"/></context></soap:Header><soap:Body><SearchResponse more="0" sortBy="dateDesc" offset="0" xmlns="urn:zimbraMail"><cn fileAsStr="Contact, Test" id="206622" rev="558980" d="1272466487000" sf="1272466487000" l="7"><a n="lastName">Contact</a><a n="company">A Nice Company</a><a n="workPhone">12345</a><a n="firstName">Test</a><a n="jobTitle">Some Fun Job</a></cn></SearchResponse></soap:Body></soap:Envelope>
    Using a PHP function I found it turns the above SOAP XML response into the following array which is pretty hard to work on
    Code:
    Array
    (
        [a] => Array
            (
                [0] => Contact
                [1] => A Nice Company
                [0_attr] => Array
                    (
                        [n] => lastName
                    )
    
                [1_attr] => Array
                    (
                        [n] => company
                    )
    
                [2] => 12345
                [2_attr] => Array
                    (
                        [n] => workPhone
                    )
    
                [3] => Test
                [3_attr] => Array
                    (
                        [n] => firstName
                    )
    
                [4] => Some Fun Job
                [4_attr] => Array
                    (
                        [n] => jobTitle
                    )
    
            )
    
    )
    Any pointers would be gratefully appreciated; even if the response is switch to JSON instead of XML.

  2. #2
    Krishopper is offline Dedicated Member
    Join Date
    Dec 2006
    Location
    Minneapolis MN
    Posts
    777
    Rep Power
    9

    Default

    For PHP, JSON is a whole lot easier and less of a headache. Results will come back to you already in an associative array.

  3. #3
    uxbod's Avatar
    uxbod is offline Moderator
    Join Date
    Nov 2006
    Location
    UK
    Posts
    8,017
    Rep Power
    24

    Default

    Thanks ... would you care to share a little bit of code for how to construct a JSON request in PHP please ?

  4. #4
    uxbod's Avatar
    uxbod is offline Moderator
    Join Date
    Nov 2006
    Location
    UK
    Posts
    8,017
    Rep Power
    24

    Default

    Krishopper, I tried something like this
    Code:
    $params = array ('Header','context','_jsns' => 'urn:zimbraAccount','Body','AuthRequest','_jsns' => 'urn:zimbra','account' => 'uxbod@domain.com','preauth' => 'timestamp=' . "$timestamp" . '" expires="0"' . $preauthToken);
    
            try
            {
                    $zimbraSession = $client->__soapCall(NULL, json_encode($params), NULL);
    
            } catch (SoapFault $exception) { echo "Failed : $exception\n\n"; }
    Though it falls in a heap due to the second element of the soapCall requiring an array. A little example would be very appreciative please ?

  5. #5
    Krishopper is offline Dedicated Member
    Join Date
    Dec 2006
    Location
    Minneapolis MN
    Posts
    777
    Rep Power
    9

    Default

    I have a larger Zimbra class that I created, but am not quite ready to share with the world, so here are bits and pieces to get an AuthTok and create an account. Hope it helps.

    Code:
    $adminUser = "user@host.com";
    $adminPass = "password";
    $adminServer = "zenoss.example.com";
    
    // Auth to the server and get auth token
    $data = array(
        "Header" => array(
            "_jsns" => "urn:zimbra",
            "session" => null,
        ),
        "Body" => array(
            "AuthRequest" => array(
                "_jsns" => "urn:zimbraAdmin",
                "name" => $adminUser,
                "password" => $adminPass,
            ),
        ),
    );
    
    $ret = dorequest($data);
    $authtok = $ret['authToken'][0]['_content'];
    
    
        /********************************************************************
         * Zimbra: CreateAccountRequest
         * arr = array(
         *     'name' => email address to create
         *    'password' => password to set on new account
         * );
         */
    
    function zimbra_CreateAccountRequest($authtok, $arr) {
        $data = array(
             "Header" => array(
                  "context" => array(
                       "_jsns" => "urn:zimbra",
                       "authToken" => array(
                            "_content" => $authtok,
                       ),
                  ),
             ),
             "Body" => array(
                  "CreateAccountRequest" => array(
                       "_jsns" => "urn:zimbraAdmin",
                  ),
             ),
        );
    
        foreach ($arr as $key => $val) {
            if ($key == 'a') {
                $data['Body']['CreateAccountRequest'][$key] = $val;
            } else {
                $data['Body']['CreateAccountRequest'][$key] = 
                    array('_content' => $val);
            }
        }
        
        print_r($data);
        $acct = array();
        $rv = $dorequest($data);
        return $rv;
    }
    
    
    
    function dorequest($data, $admin=true, $strip=true) {
        $js = json_encode($data);
        $url = ($admin ?
            sprintf("https://%s:7071/service/admin/soap/", $adminServer) :
            sprintf("https://%s/service/soap/", $adminServer)
        );
    
        $ch = curl_init();
    
        $options = array(
            CURLOPT_URL => $url,
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $js,
            CURLOPT_HEADER => false,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_RETURNTRANSFER => true,
        );
    
        curl_setopt_array($ch, $options);
        $output = curl_exec($ch);
        curl_close($ch);
    
        if ($output === false) { print curl_error($ch); }
    
        $outputarr = json_decode($output);
        $arr = $this->object2array($outputarr);
    
        if (!$strip) { return $arr; }
        $arr = array_shift($arr['Body']);
        return $arr;
    
    }

  6. #6
    uxbod's Avatar
    uxbod is offline Moderator
    Join Date
    Nov 2006
    Location
    UK
    Posts
    8,017
    Rep Power
    24

    Default

    Krishopper, many thanks for that I believe I am so close now! When formatting the preauth using a non admin account I am ending up with an array that looks like
    Code:
    Array
    (
        [Header] => Array
            (
                [_jsns] => urn:zimbraAccount
            )
    
        [Body] => Array
            (
                [AuthRequest] => Array
                    (
                        [_jsns] => urn:zimbraAccount
                        [account] => uxbod@domain.com
                        [preauth timestamp=1272612560000 expires=0] => Array
                            (
                                [_content] => 525b9ace76ac211077032e404c695b93ff00ee12
                            )
    
                    )
    
            )
    
    )
    Does that looks right ? I know that if you were doing it via XML the preauth field should look like
    Code:
    <preauth timestamp="1272612560000" expires="0">525b9ace76ac211077032e404c695b93ff00ee12</preauth>
    And I am thinking that my array for JSON encoding is not quite right yet ?

  7. #7
    uxbod's Avatar
    uxbod is offline Moderator
    Join Date
    Nov 2006
    Location
    UK
    Posts
    8,017
    Rep Power
    24

    Default

    Worked it out Krishopper, and using curl is definately a better option than SoapClient! Thank you so so much for your help it has been very appreciated

  8. #8
    Krishopper is offline Dedicated Member
    Join Date
    Dec 2006
    Location
    Minneapolis MN
    Posts
    777
    Rep Power
    9

    Default

    No problem - and CURL + JSON does make it a whole heck of a lot easier in PHP. It almost seems too easy. I fought with SOAP+PHP for a while and then noticed that Zimbra supported JSON after reading soap.txt and soap-admin.txt in the doc directory, gave it a try, and was on my happy way.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Can recives but can't send mail outgoing
    By hoangkk in forum Installation
    Replies: 9
    Last Post: 09-21-2009, 07:35 PM
  2. Replies: 35
    Last Post: 07-17-2009, 03:52 PM
  3. zmzimletctl deploy zimbra_posixaccount.zip - ERROR -
    By todd_dsm in forum Administrators
    Replies: 0
    Last Post: 04-02-2009, 01:41 PM
  4. March 6 EDU Summit @ Yahoo UK
    By mmorse in forum Zimbra in Education
    Replies: 0
    Last Post: 02-25-2009, 12:22 PM
  5. Zimbra @ Educause - Orlando
    By justcdl123 in forum Zimbra in Education
    Replies: 0
    Last Post: 08-19-2008, 12:15 PM

Posting Permissions

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