I hope this helps someone ... here is the code I hacked together to login as an admin and create a user. Obviously, you need to make this work for your environment (server, usernames, etc.), and break it up as you see fit. But it took me all day to get to this point. No reason someone else should have to do the same...
I've put '!!' wherever I think you will have to change a value to work in your environment.
WARNING: Lots and lots of debug (echos) included. You'll get to know more than you may have ever wanted to about the way the SOAP envelopes look...
First off, you want to create this to AuthRequest:
Code:
<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>admin@mydomain.com</name>
<password>mystrongpassword</password>
</AuthRequest>
</soap:Body>
</soap:Envelope>
And here's how I did it:
Code:
<?php
$client = new SoapClient(null,
array(
'location' => "https://!!mail.yourdomain.tld!!:7071/service/admin/soap/",
'uri' => "urn:zimbraAdmin",
'trace' => 1,
'exceptions' => 1,
'soap_version' => SOAP_1_1,
'style' => SOAP_RPC,
'use' => SOAP_LITERAL
)
);
echo "Client Built<br>";
$params = array(
new SoapParam("!!adminusername!!", "name"),
new SoapParam("!!adminpassword!!", "password")
);
echo "Params built<br>";
try {
echo "creating header<br>";
$soapHeader = new SoapHeader(
'urn:zimbra',
'context'
);
echo "trying<br>";
$result = $client->__soapCall(
"AuthRequest",
$params,
null,
$soapHeader
);
echo "executed<br>";
} catch (SoapFault $exception) {
echo "exception caught<br><br>";
echo htmlentities($client->__getLastRequest()) . "<br><br>";
echo htmlentities($client->__getLastRequestHeaders()) . "<br><br>";
echo htmlentities($client->__getLastResponseHeaders()) . "<br><br>";
echo htmlentities($client->__getLastResponse()) . "<br><br>";
echo $exception . "<br><br>";
}
echo "done<br><br>";
echo htmlentities($client->__getLastRequest()) . "<br><br>";
echo htmlentities($client->__getLastResponse()) . "<br><br>";
var_dump($result);
echo "<br><br>";
echo $result['authToken'] . "<br><br>";
echo "logged in, continuing<br><Br>";
$authToken=$result['authToken']; Next, you want to create the CreateAccountRequest that should look like this:
Code:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<context xmlns="urn:zimbra">
<authToken>authToken goes here</authToken>
</context>
</soap:Header>
<soap:Body>
<CreateAccountRequest xmlns=\"urn:zimbraAdmin\">
<name>test@mydomain.com</name>
<password>myverystrongpassword</password>
</CreateAccountRequest>
</soap:Body>
</soap:Envelope>
So, here's how I did that:
Code:
echo "creating new user params<br><br>";
$params = array(
new SoapParam("!!test@<domain>.com!!", "name"),
new SoapParam("!!newpassword!!", "password")
);
echo "new user Params built<br>";
try {
echo "creating new user header<br>";
$soapHeader = array(
new SoapHeader(
'urn:zimbra',
'context',
new SoapVar(
'<ns2:context><authToken>' . $authToken . '</authToken></ns2:context>',
XSD_ANYXML
)
)
);
echo "trying new user<br>";
$result = $client->__soapCall(
"CreateAccountRequest",
$params,
null,
$soapHeader
);
echo "executed new user creation<br>";
} catch (SoapFault $exception) {
echo "exception caught<br><br>";
echo htmlentities($client->__getLastRequest()) . "<br><br>";
echo htmlentities($client->__getLastRequestHeaders()) . "<br><br>";
echo htmlentities($client->__getLastResponseHeaders()) . "<br><br>";
echo htmlentities($client->__getLastResponse()) . "<br><br>";
echo $exception . "<br><br>";
}
echo "created user<br><br>";
echo htmlentities($client->__getLastRequest()) . "<br><br>";
echo htmlentities($client->__getLastResponse()) . "<br><br>";
var_dump($result);
echo "<br><br>";
echo $result['account'] . "<br><br>";
echo "user created!<br><Br>";
?> Keep in mind that this was one .php page for me, therefore I could use the same $client and $authToken objects between the login and user creation.
Feel free to ask me questions, though keep in mind that I hacked this together - literally!