I am working with a SOAP API that no matter how I try I am unable to get it working with PHP ( I was told by others working with this API that they were not able to get it working with any other language other than Perl which is what it is written in). I had to "hack" a bit so PHP could handle it correctly.
The code I am using is:
<?PHP
$uid = 'username';
$pass = 'password';
$customer_xml = my_api($uid, $pass, 'get_numbers', 'number', 'contains', '1', '0', '0');
function my_api($user, $pass, $func, $request_args){
$soap = new SoapClient("admin/enswitch_3.9.wsdl", array('trace' => true, 'exceptions' => false));
$result1 = $soap->$func($user,$pass,$request_args);
if(isset($soap->__soap_fault)){
echo 'There was an error connecting via SOAP. Below is the error:';
print_r($soap->__soap_fault->faultstring);
}
elseif(isset($soap->__last_response)){
echo "Unedited soap response is:
";
echo $soap->__last_response."
";
echo "
";
$xml_string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $soap->__last_response);
//We need to replace the text s-gensym and then the number with some random text
//like xoxo so we can then get the value of $xml->response->xoxo which should be an array
$xml_string = preg_replace('/s-gensym[0-9]{0,65000}/', 'xoxo', $xml_string);
$xml = new SimpleXMLElement($xml_string);
$xml = $xml->soapBody;
$func_response = $func.'Response';
echo '$xml->$func_response is'."
";
print_r($xml->$func_response);
echo "
";
echo '$xml->$func_response->xoxo is'."
";
print_r( $xml->$func_response->xoxo);
die();
return $xml->$func_response->xoxo;
}
}
?>
The response that I get from my script code is (phone numbers have been changed to protect them):
Unedited soap response is:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><get_numbersResponse xmlns="http://www.example.com/Integrics/Enswitch/API"><s-gensym3><maximum_seconds xsi:type="xsd:string">-1</maximum_seconds><direct xsi:type="xsd:string">1</direct><fax_dtype xsi:type="xsd:string">hangup</fax_dtype><stype xsi:type="xsd:string">number</stype><emergency_register xsi:type="xsd:string">0</emergency_register><directory xsi:type="xsd:string">1</directory><display xsi:type="xsd:string">12125550432</display><sname_action xsi:type="xsd:string">replace</sname_action><shared xsi:type="xsd:string">0</shared><callerid xsi:type="xsd:string" /><dtype xsi:type="xsd:string">queue</dtype><sname xsi:type="xsd:string" /><dnumber xsi:type="xsd:string">696</dnumber><fax_detect xsi:type="xsd:string">0</fax_detect><music xsi:type="xsd:string">0</music><update_routing xsi:type="xsd:string">1</update_routing><routing xsi:type="xsd:string" /><owner xsi:type="xsd:string">0</owner><ported xsi:type="xsd:string">0</ported><language xsi:type="xsd:string">en</language><fax_dnumber xsi:type="xsd:string" /><description xsi:type="xsd:string" /><ingroup xsi:type="xsd:string">12</ingroup><play_message xsi:type="xsd:string">0</play_message><screen xsi:type="xsd:string">0</screen><customer xsi:type="xsd:string">3935</customer><recordgroup xsi:type="xsd:string">0</recordgroup><class xsi:type="xsd:string">212</class><shortcut xsi:type="xsd:string">1</shortcut><alias xsi:type="xsd:string" /><snumber xsi:type="xsd:string">12125550432</snumber></s-gensym3><s-gensym5><maximum_seconds xsi:type="xsd:string">-1</maximum_seconds><direct xsi:type="xsd:string">1</direct><fax_dtype xsi:type="xsd:string">hangup</fax_dtype><stype xsi:type="xsd:string">number</stype><emergency_register xsi:type="xsd:string">0</emergency_register><directory xsi:type="xsd:string">1</directory><display xsi:type="xsd:string">12125550458 TEST FOR COLLECT CALL DISABLING</display><sname_action xsi:type="xsd:string">replace</sname_action><shared xsi:type="xsd:string">0</shared><callerid xsi:type="xsd:string" /><dtype xsi:type="xsd:string">forward</dtype><sname xsi:type="xsd:string" /><dnumber xsi:type="xsd:string">18005551212</dnumber><fax_detect xsi:type="xsd:string">0</fax_detect><music xsi:type="xsd:string">0</music><update_routing xsi:type="xsd:string">0</update_routing><routing xsi:type="xsd:string" /><owner xsi:type="xsd:string">0</owner><ported xsi:type="xsd:string">0</ported><language xsi:type="xsd:string">en</language><fax_dnumber xsi:type="xsd:string" /><description xsi:type="xsd:string">TEST FOR COLLECT CALL DISABLING</description><ingroup xsi:type="xsd:string">12</ingroup><play_message xsi:type="xsd:string">0</play_message><screen xsi:type="xsd:string">0</screen><customer xsi:type="xsd:string">3935</customer><recordgroup xsi:type="xsd:string">0</recordgroup><class xsi:type="xsd:string">212</class><shortcut xsi:type="xsd:string">1</shortcut><alias xsi:type="xsd:string" /><snumber xsi:type="xsd:string">12125550458</snumber></s-gensym5></get_numbersResponse></soap:Body></soap:Envelope>
$xml->$func_response is
SimpleXMLElement Object
(
[xoxo] => Array
(
[0] => SimpleXMLElement Object
(
[maximum_seconds] => -1
[direct] => 1
[fax_dtype] => hangup
[stype] => number
[emergency_register] => 0
[directory] => 1
[display] => 12125550432
[sname_action] => replace
[shared] => 0
[callerid] => SimpleXMLElement Object
(
)
[dtype] => queue
[sname] => SimpleXMLElement Object
(
)
[dnumber] => 696
[fax_detect] => 0
[music] => 0
[update_routing] => 1
[routing] => SimpleXMLElement Object
(
)
[owner] => 0
[ported] => 0
[language] => en
[fax_dnumber] => SimpleXMLElement Object
(
)
[description] => SimpleXMLElement Object
(
)
[ingroup] => 12
[play_message] => 0
[screen] => 0
[customer] => 3935
[recordgroup] => 0
[class] => 212
[shortcut] => 1
[alias] => SimpleXMLElement Object
(
)
[snumber] => 12125550432
)
[1] => SimpleXMLElement Object
(
[maximum_seconds] => -1
[direct] => 1
[fax_dtype] => hangup
[stype] => number
[emergency_register] => 0
[directory] => 1
[display] => 12125550458 TEST FOR COLLECT CALL DISABLING
[sname_action] => replace
[shared] => 0
[callerid] => SimpleXMLElement Object
(
)
[dtype] => forward
[sname] => SimpleXMLElement Object
(
)
[dnumber] => 18005551212
[fax_detect] => 0
[music] => 0
[update_routing] => 0
[routing] => SimpleXMLElement Object
(
)
[owner] => 0
[ported] => 0
[language] => en
[fax_dnumber] => SimpleXMLElement Object
(
)
[description] => TEST FOR COLLECT CALL DISABLING
[ingroup] => 12
[play_message] => 0
[screen] => 0
[customer] => 3935
[recordgroup] => 0
[class] => 212
[shortcut] => 1
[alias] => SimpleXMLElement Object
(
)
[snumber] => 12125550458
)
)
)
$xml->$func_response->xoxo is
SimpleXMLElement Object
(
[maximum_seconds] => -1
[direct] => 1
[fax_dtype] => hangup
[stype] => number
[emergency_register] => 0
[directory] => 1
[display] => 12125550432
[sname_action] => replace
[shared] => 0
[callerid] => SimpleXMLElement Object
(
)
[dtype] => queue
[sname] => SimpleXMLElement Object
(
)
[dnumber] => 696
[fax_detect] => 0
[music] => 0
[update_routing] => 1
[routing] => SimpleXMLElement Object
(
)
[owner] => 0
[ported] => 0
[language] => en
[fax_dnumber] => SimpleXMLElement Object
(
)
[description] => SimpleXMLElement Object
(
)
[ingroup] => 12
[play_message] => 0
[screen] => 0
[customer] => 3935
[recordgroup] => 0
[class] => 212
[shortcut] => 1
[alias] => SimpleXMLElement Object
(
)
[snumber] => 12125550432
)
My question is how come $xml->$func_response will show multiple responses under "element" xoxo but if I try to print_r on $xml->func_response->xoxo I only get one response. Interesting enough if I do:
foreach($xml->func_response as $y){
foreach($y as $z){
print_r($z)
}
}
}
Then I do get the value for each one but it seems backward to do it that way.
Reformatted XML data
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<get_numbersResponse xmlns="http://www.example.com/Integrics/Enswitch/API">
<s-gensym3>
<maximum_seconds xsi:type="xsd:string">-1</maximum_seconds>
<direct xsi:type="xsd:string">1</direct>
<fax_dtype xsi:type="xsd:string">hangup</fax_dtype>
<stype xsi:type="xsd:string">number</stype>
<emergency_register xsi:type="xsd:string">0</emergency_register>
<directory xsi:type="xsd:string">1</directory>
<display xsi:type="xsd:string">12125550432</display>
<sname_action xsi:type="xsd:string">replace</sname_action>
<shared xsi:type="xsd:string">0</shared>
<callerid xsi:type="xsd:string"/>
<dtype xsi:type="xsd:string">queue</dtype>
<sname xsi:type="xsd:string"/>
<dnumber xsi:type="xsd:string">696</dnumber>
<fax_detect xsi:type="xsd:string">0</fax_detect>
<music xsi:type="xsd:string">0</music>
<update_routing xsi:type="xsd:string">1</update_routing>
<routing xsi:type="xsd:string"/>
<owner xsi:type="xsd:string">0</owner>
<ported xsi:type="xsd:string">0</ported>
<language xsi:type="xsd:string">en</language>
<fax_dnumber xsi:type="xsd:string"/>
<description xsi:type="xsd:string"/>
<ingroup xsi:type="xsd:string">12</ingroup>
<play_message xsi:type="xsd:string">0</play_message>
<screen xsi:type="xsd:string">0</screen>
<customer xsi:type="xsd:string">3935</customer>
<recordgroup xsi:type="xsd:string">0</recordgroup>
<class xsi:type="xsd:string">212</class>
<shortcut xsi:type="xsd:string">1</shortcut>
<alias xsi:type="xsd:string"/>
<snumber xsi:type="xsd:string">12125550432</snumber>
</s-gensym3>
<s-gensym5>
<maximum_seconds xsi:type="xsd:string">-1</maximum_seconds>
<direct xsi:type="xsd:string">1</direct>
<fax_dtype xsi:type="xsd:string">hangup</fax_dtype>
<stype xsi:type="xsd:string">number</stype>
<emergency_register xsi:type="xsd:string">0</emergency_register>
<directory xsi:type="xsd:string">1</directory>
<display xsi:type="xsd:string">12125550458 TEST FOR COLLECT CALL DISABLING</display>
<sname_action xsi:type="xsd:string">replace</sname_action>
<shared xsi:type="xsd:string">0</shared>
<callerid xsi:type="xsd:string"/>
<dtype xsi:type="xsd:string">forward</dtype>
<sname xsi:type="xsd:string"/>
<dnumber xsi:type="xsd:string">18005551212</dnumber>
<fax_detect xsi:type="xsd:string">0</fax_detect>
<music xsi:type="xsd:string">0</music>
<update_routing xsi:type="xsd:string">0</update_routing>
<routing xsi:type="xsd:string"/>
<owner xsi:type="xsd:string">0</owner>
<ported xsi:type="xsd:string">0</ported>
<language xsi:type="xsd:string">en</language>
<fax_dnumber xsi:type="xsd:string"/>
<description xsi:type="xsd:string">TEST FOR COLLECT CALL DISABLING</description>
<ingroup xsi:type="xsd:string">12</ingroup>
<play_message xsi:type="xsd:string">0</play_message>
<screen xsi:type="xsd:string">0</screen>
<customer xsi:type="xsd:string">3935</customer>
<recordgroup xsi:type="xsd:string">0</recordgroup>
<class xsi:type="xsd:string">212</class>
<shortcut xsi:type="xsd:string">1</shortcut>
<alias xsi:type="xsd:string"/>
<snumber xsi:type="xsd:string">12125550458</snumber>
</s-gensym5>
</get_numbersResponse>
</soap:Body>
</soap:Envelope>