从PHP中的SoapClient返回检索特定值 [英] Retrieving specific values from a SoapClient Return in PHP

查看:40
本文介绍了从PHP中的SoapClient返回检索特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP 5.3.1调用Web服务,我的请求看起来是这样的:

<?php

$client = new SoapClient('the API wsdl'); 
$param = array(
    'LicenseKey' => 'a guid'
  ); 


$result = $client->GetUnreadIncomingMessages($param);

echo "<pre>";
print_r($result);
echo "</pre>";

?>

以下是我得到的回复:

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
                    )

            )

    )

)

推荐答案

若要获取要检索的数据,您需要导航多个嵌套对象。这些对象是stdClass类型。我的理解是您可以访问stdClass中的嵌套对象,但我打算将它们强制转换为数组,以使索引更容易。

因此从:

开始
<?php     

$client = new SoapClient('the API wsdl');      
$param = array('LicenseKey' => 'a guid');      
$result = $client->GetUnreadIncomingMessages($param);  
您现在有了一个类型为stdClass的$result变量。其中有一个名为"GetUnreadIncomingMessagesResult"的stdClass类型的对象。该对象又包含一个名为"SMSIncomingMessage"的数组。该数组包含数量可变的stdClass对象,这些对象保存您需要的数据。

因此我们执行以下操作:

$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>";

                }                   
?>

这应该会给出您正在寻找的输出。您可以调整索引holdingArray的位置,以获取您要查找的任何特定信息。

完整代码如下:

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

这篇关于从PHP中的SoapClient返回检索特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆