加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从PHP中的SoapClient返回中检索特定值

发布时间:2020-12-13 17:21:18 所属栏目:PHP教程 来源:网络整理
导读:我使用 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";? 以下是我回复
我使用 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 vavialble.这里有一个stdClass类型的对象叫做“GetUnreadIncomingMessagesResult”.该对象又包含一个名为“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>";
    }
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读