Getting specific values ​​from SoapClient Return in PHP

I call webservice with PHP 5.3.1 and my request looks like this:

<?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 answer that I will return:

 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 ) ) ) ) 
+4
source share
1 answer

To get the data that you want to receive, you need to navigate through several nested objects. Objects are of type stdClass. I understand that you can access nested objects in stdClass, but I'm going to assign 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 $ result vavialble of type stdClass. This object also has one stdClass object called GetUnreadIncomingMessagesResult. This object, in turn, contains an array called "SMSIncomingMessage". This array contains a variable number of stdClass objects that contain the desired data.

So we do the following:

 $outterArray = ((array)$result); $innerArray = ((array)$outterArray['GetUnreadIncomingMessagesResult']); $dataArray = ((array)$innerArray['SMSIncomingMessage']); 

Now we have an array containing each object from which we want to extract data. Therefore, we iterate over this array to obtain a holder object, drop the storage object into an array, and extract the necessary information. You do it like this:

 foreach($dataArray as $holdingObject) { $holdingArray = ((array)$holdingObject); $phoneNum = $holdingArray['FromPhoneNumber']; $message = $holdingArray['Message']; echo"<div>$fphone</div> <div>$message</div>"; } ?> 

This will give you the result you are looking for. You can customize where you index holdArray to get any specific information you are looking for.

The full code is as follows:

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

Source: https://habr.com/ru/post/1435394/


All Articles