I call an webservice using PHP 5.3.1 and my request looks as so:
<?php
$client = new SoapClient('the API wsdl');
$param = array(
'LicenseKey' => 'a guid'
);
$result = $client->GetUnreadIncomingMessages($param);
echo "<pre>";
print_r($result);
echo "</pre>";
?>
Here is the response that I get back:
stdClass Object
(
[GetUnreadIncomingMessagesResult] => stdClass Object
(
[SMSIncomingMessage] => Array
(
[0] => stdClass Object
(
[FromPhoneNumber] => the number
[IncomingMessageID] => message ID
[MatchedMessageID] =>
[Message] => Hello there
[ResponseReceiveDate] => 2012-09-20T20:42:14.38
[ToPhoneNumber] => another number
)
[1] => stdClass Object
(
[FromPhoneNumber] => the number
[IncomingMessageID] =>
[MatchedMessageID] =>
[Message] => hello again
[ResponseReceiveDate] => 2012-09-20T20:42:20.69
[ToPhoneNumber] => another number
)
)
)
)
To get to the data you want to retrieve you need to navigate through multiple nested objects. The objects are of stdClass type. It is my understanding that you can access nested objects in a stdClass but I am going to cast them to arrays to make indexing easier.
So start with:
<?php
$client = new SoapClient('the API wsdl');
$param = array('LicenseKey' => 'a guid');
$result = $client->GetUnreadIncomingMessages($param);
You now have a $result vavialble of type stdClass. This has a single object in it also of type stdClass called "GetUnreadIncomingMessagesResult." This Object in turn contains an array called "SMSIncomingMessage." That array contains a varriable number of objects of stdClass that hold the data you want.
So we do the following:
$outterArray = ((array)$result);
$innerArray = ((array)$outterArray['GetUnreadIncomingMessagesResult']);
$dataArray = ((array)$innerArray['SMSIncomingMessage']);
Now we have an array holding each object we want to extract data from. So we loop through this array to get the holding object, cast the holding object to an array and than extract the necessary information. You do this as follows:
foreach($dataArray as $holdingObject)
{
$holdingArray = ((array)$holdingObject);
$phoneNum = $holdingArray['FromPhoneNumber'];
$message = $holdingArray['Message'];
echo"<div>$fphone</div>
<div>$message</div>";
}
?>
This should give you the output you are looking for. You can adjust where you index the holdingArray to get out whatever specific information you are looking for.
The complete code looks like:
<?php
$client = new SoapClient('the API wsdl');
$param = array('LicenseKey' => 'a guid');
$result = $client->GetUnreadIncomingMessages($param);
$outterArray = ((array)$result);
$innerArray = ((array)$outterArray['GetUnreadIncomingMessagesResult']);
$dataArray = ((array)$innerArray['SMSIncomingMessage']);
foreach($dataArray as $holdingObject)
{
$holdingArray = ((array)$holdingObject);
$phoneNum = $holdingArray['FromPhoneNumber'];
$message = $holdingArray['Message'];
echo"<div>$fphone</div>
<div>$message</div>";
}
?>