Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: [SOLVED] Simple SOAP Admin Example In PHP

  1. #11
    meisel is offline Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    4

    Default

    Quote Originally Posted by mikes2277 View Post
    Well for those wondering the PHP SOAP functions really are not doing anything but making is really hard to do a simple curl HTTP POST to Zimbra's SOAP URL. Doing it the following way makes it so you can do "zmprov --debug whatever" and be able to use that trace to get done exactly the same thing in PHP. In the example below you could technically break it up into 2 functions and have the login happen in an isolated function so that it doesn't create a ton of admin sessions sitting out there every time you add an account since one authToken & sessionId can be used for an infinite amount of SOAP requests. But for my purposes my PHP script is very short lived and literally just adds the account and exits so it is not needed.

    Code:
    <?PHP
            $ServerAddress          = "192.168.1.1";
            $AdminUserName          = "admin";
            $AdminPassword          = "adminpassword";
    
            $NewUserName            = "mynewaccount@mydomain.com";
            $NewUserPassword        = "mynewpassoword";
            $COSId                  = "f070eede-c0c5-4867-a158-1f35f1c39e15";
    
            // To get your COSId for COS defualt or any other COS (I left mine in there as an example):
            // zmprov getCos default | grep "zimbraId:" from the command line
    
            if(($ZimbraSOAPResponse = ZimbraAdminCreateAccount(1, $ServerAddress, $AdminUserName, $AdminPassword, $NewUserName, $NewUserPassword, $COSId)) == FALSE)
            {
                    printf("ZimbraAdminCreateAccount Failed!<BR>\n");
                    return(FALSE);
            }
    
            print("Zimbra create account response:<BR>" . htmlentities($ZimbraSOAPResponse) . "<BR><BR>\n");
    ?>
    
    <?PHP
    
    // -------------------------------------------------------------------
    
    function ZimbraAdminCreateAccount($Trace, $ServerAddress, $AdminUserName, $AdminPassword, $NewUserName, $NewPassword, $COSId)
    {
            $CurlHandle = curl_init();
            curl_setopt($CurlHandle, CURLOPT_URL,           "https://$ServerAddress:7071/service/admin/soap");
            curl_setopt($CurlHandle, CURLOPT_POST,           TRUE);
            curl_setopt($CurlHandle, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($CurlHandle, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($CurlHandle, CURLOPT_SSL_VERIFYHOST, FALSE);
    
            // ------ Send the zimbraAdmin AuthRequest -----
    
            $SOAPMessage  = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
                                    <soap:Header>
                                            <context xmlns="urn:zimbra"/>
                                    </soap:Header>
                                    <soap:Body>
                                            <AuthRequest xmlns="urn:zimbraAdmin">
                                                    <name>' . $AdminUserName . '</name>
                                                    <password>' . $AdminPassword . '</password>
                                            </AuthRequest>
                                    </soap:Body>
                            </soap:Envelope>';
    
            curl_setopt($CurlHandle, CURLOPT_POSTFIELDS, $SOAPMessage);
    
            if(!($ZimbraSOAPResponse = curl_exec($CurlHandle)))
            {
                    print("ERROR: curl_exec - (" . curl_errno($CurlHandle) . ") " . curl_error($CurlHandle));
                    return(FALSE);
            }
    
            // print("Raw Zimbra SOAP Response:<BR>" . htmlentities($ZimbraSOAPResponse) . "<BR><BR>\n");
    
            // Parse for the sessionId
            // <sessionId type="admin" id="123">123</sessionId>
            $sessionId = strstr($ZimbraSOAPResponse, "<sessionId");
            $sessionId = strstr($sessionId, ">");
            $sessionId = substr($sessionId, 1, strpos($sessionId, "<") - 1);
            // print("sessionId = $sessionId<BR>\n");
    
            // Parse for the authToken
            // <authToken>123</authToken>
            $authToken = strstr($ZimbraSOAPResponse, "<authToken");
            $authToken = strstr($authToken, ">");
            $authToken = substr($authToken, 1, strpos($authToken, "<") - 1);
            // print("authToken = $authToken<BR>\n");
    
    
            // ------ Send the zimbraCreateAccount request -----
            $SOAPMessage = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
                                    <soap:Header>
                                            <context xmlns="urn:zimbra">
                                                    <authToken>' . $authToken . '</authToken>
                                                    <sessionId id="' . $sessionId . '">' . $sessionId . '</sessionId>
                                            </context>
                                    </soap:Header>
                                    <soap:Body>
                                            <CreateAccountRequest xmlns="urn:zimbraAdmin">
                                                    <name>' . $NewUserName . '</name>
                                                    <password>' . $NewUserPassword . '</password>
                                                    <a n="zimbraCOSId">' . $COSId . '</a>
                                            </CreateAccountRequest>
                                    </soap:Body>
                            </soap:Envelope>';
    
            curl_setopt($CurlHandle, CURLOPT_POSTFIELDS, $SOAPMessage);
    
            if(!($ZimbraSOAPResponse = curl_exec($CurlHandle)))
            {
                    print("ERROR: curl_exec - (" . curl_errno($CurlHandle) . ") " . curl_error($CurlHandle));
                    return(FALSE);
            }
    
            // print("Raw Zimbra SOAP Response:<BR>" . htmlentities($ZimbraSOAPResponse) . "<BR><BR>\n");
    
            return($ZimbraSOAPResponse);
    }
    ?>

    SUCCESSFULLY create a new account with script above,,
    but, user can't login with the password,,
    can u help me ??
    Last edited by meisel; 07-23-2011 at 12:59 AM.

  2. #12
    meisel is offline Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    4

    Default

    can anyone give me script for change user password from SOAP??
    Last edited by meisel; 07-23-2011 at 02:15 AM.

Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. PHP SOAP vs Zimbra
    By Corey Scott in forum Developers
    Replies: 14
    Last Post: 02-05-2011, 04:41 AM
  2. PHP SOAP create appointment example
    By AddGun in forum Developers
    Replies: 13
    Last Post: 09-27-2010, 04:41 PM
  3. Soap search admin mode
    By lfasci in forum Developers
    Replies: 0
    Last Post: 07-09-2007, 09:04 AM
  4. php CreateAccountRequest via SOAP
    By bookatechie in forum Developers
    Replies: 2
    Last Post: 05-02-2007, 11:28 AM
  5. Replies: 1
    Last Post: 09-19-2006, 10:56 AM

Posting Permissions

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