Retrieving specific values from a SoapClient Return in PHP

南笙酒味 提交于 2019-12-10 09:23:24

问题


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
                    )

            )

    )

)

回答1:


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>";
    }
?>


来源:https://stackoverflow.com/questions/12521545/retrieving-specific-values-from-a-soapclient-return-in-php

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