在PHP中从SoapClient返回检索特定值
我使用PHP 5.3.1调用Web服务,我的请求如下所示:在PHP中从SoapClient返回检索特定值,php,soap,wamp,soap-client,Php,Soap,Wamp,Soap Client,我使用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
$client = new SoapClient('the API wsdl');
$param = array(
'LicenseKey' => 'a guid'
);
$result = $client->GetUnreadIncomingMessages($param);
echo "<pre>";
print_r($result);
echo "</pre>";
?>
要获取要检索的数据,需要在多个嵌套对象中导航。对象是stdClass类型。我的理解是,您可以访问stdClass中的嵌套对象,但我将把它们转换为数组,以使索引更容易 因此,首先:
$outterArray = ((array)$result);
$innerArray = ((array)$outterArray['GetUnreadIncomingMessagesResult']);
$dataArray = ((array)$innerArray['SMSIncomingMessage']);
现在我们有了一个数组,其中包含了我们要从中提取数据的每个对象。因此,我们循环通过这个数组来获取保持对象,将保持对象转换为一个数组,然后提取必要的信息。您可以按如下方式执行此操作:
<?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>";
}
?>
foreach($dataArray作为$holdingObject)
{
$holdingArray=((数组)$holdingObject);
$phoneNum=$holdingArray['FromPhoneNumber'];
$message=$holdingArray['message'];
回声“$fphone
$message”;
}
?>
这将为您提供所需的输出。您可以调整索引保留阵列的位置,以获取您要查找的任何特定信息
完整的代码如下所示:
谢谢Tarynn。这太棒了!现在我要做的就是一些小的造型。完美的
$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
$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>";
}
?>