Retrieving specific values from a SoapClient Return in PHP

China☆狼群 提交于 2019-12-05 13:28:26

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>";
    }
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!