Reputation: 32272
I am trying to write some code to talk to the SmarterMail API, but I can't seem to make PHP format the request properly. The [ugly] code I have so far is:
<?php
function array_to_params($in_arr) {
$out_arr = array();
foreach( $in_arr as $key => $value ) {
$out_arr[] = new SoapParam($value, $key);
}
return $out_arr;
}
echo "<pre>";
$soapClient = new SoapClient(
"http://mailserver.net/Services/svcDomainAdmin.asmx?WSDL",
array('trace' => true)
);
var_dump($soapClient->__getTypes());die();
$soap_user_params = array(
'blank' => 'blank', //if I don't have a dummy entry the first parameter doesn't show up.
'AuthUserName' => 'admin',
'AuthPassword' => 'derp'
);
$soap_user_params = array_to_params($soap_user_params);
$error = FALSE;
try {
$info = $soapClient->__soapCall("GetAllDomains", $soap_user_params);
// $info = $soapClient->GetAllDomains($soap_user_params); //same results
} catch (SoapFault $fault) {
$error = TRUE;
printf("Error %s: %s\n", $fault->faultcode, $fault->faultstring);
}
if( !$error ) {
var_dump($info);
echo "\nRequest: " . htmlentities($soapClient->__getLastRequest());
}
echo "</pre>";
?>
For reference, the WSDL for this function is:
<s:element name="GetAllDomains">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="AuthUserName" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="AuthPassword" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
The example request given is:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetAllDomains xmlns="http://tempuri.org/">
<AuthUserName>string</AuthUserName>
<AuthPassword>string</AuthPassword>
</GetAllDomains>
</soap:Body>
</soap:Envelope>
But the request generated by my code is:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:GetAllDomains/>
<AuthUserName>admin</AuthUserName>
<AuthPassword>derp</AuthPassword>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
You can see that the username and password parameters are not contained within the GetAllDomains section for some reason. As well, in the code you can see that I have to pass a dummy parameter in first because whatever is first does not show up in the request. Also, I cannot simply pass in an associative array because the keys are not used as parameter names, it puts in 'param1' and 'param2' instead.
I should note that the server accepts the request and spits back a message about the auth failing. There is a built-in SOAP request tester on the server, and when the request fits the given example it works normally.
Everything I've looked up in the PHP docs and through Google seems to indicate that it should not be this difficult. :(
Any help is GREATLY appreciated.
note: I am running PHP 5.3.8 on Apache 2.2 on FreeBSD, built and installed via ports today.
post-answer-edit:
Thanks to Gian's answer below I've gotten this working and greatly simplified the code:
<?php
echo "<pre>";
$soapClient = new SoapClient( "http://mailserver.net/Services/svcDomainAdmin.asmx?WSDL", array( 'trace' => true ) );
$soap_user_params = array(
'AuthUserName' => 'admin',
'AuthPassword' => 'derp'
);
try {
$info = $soapClient->__soapCall("GetAllDomains", array( 'parameters' => $soap_user_params ) );
var_dump($info);
echo "\nRequest:\n" . htmlentities($soapClient->__getLastRequest());
} catch (SoapFault $fault) {
printf("Error %s: %s\n", $fault->faultcode, $fault->faultstring);
}
echo "</pre>";
Upvotes: 1
Views: 2407
Reputation: 13955
My apologies for the mis-directed close. I misunderstood what you were trying to do at first.
Sifting through the soapCall
documentation comments, there are a couple things I'd suggest trying. First off, this:
$soap_user_params = array(
'parameters' => array (
'AuthUserName' => 'admin',
'AuthPassword' => 'derp'
)
);
Apparently your parameters need to be an element in an associative array with the key 'parameters'. Weird, huh?
Failing that, you may need to nest this even deeper! The _
array entry does not appear to be documented anywhere obvious, but this seems to be something that the commenters on the PHP docs were suggesting. So maybe this:
$soap_user_params = array(
'parameters' => array (
'AuthUserName' => array('_' => 'admin'),
'AuthPassword' => array('_' => 'derp')
)
);
Upvotes: 1