我使用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
)
)
)
)
答案 0 :(得分:5)
要获取要检索的数据,您需要浏览多个嵌套对象。对象是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>";
}
?>