16

我正在使用以下代码使用 Soap 从网站获取数据。

$client = new SoapClient('http://some.url.here');
class SMSParam {
    public $CellNumber;
    public $AccountKey;
    public $MessageCount;
    public $MessageBody;
    public $Reference;

}
$parameters = new SMSParam;
$parameters -> AccountKey = "$sms_key";
$parameters -> MessageCount = "25";
$Result = $client->GetIncomingMessages($parameters);
echo "<pre>";
print_r($Result);
echo "</pre>";

以下是输出示例:

stdClass Object
(
    [GetIncomingMessagesResult] => stdClass Object
        (
            [SMSIncomingMessage] => Array
                (
                    [0] => stdClass Object
                        (
                            [OutgoingMessageID] => data
                            [Reference] => data
                            [MessageNumber] => data
                            [PhoneNumber] => data
                            [Message] => data
                            [ReceivedDate] => data
                        )

                    [1] => stdClass Object
                        (
                            [OutgoingMessageID] => data
                            [Reference] => data
                            [MessageNumber] => data
                            [PhoneNumber] => data
                            [Message] => data
                            [ReceivedDate] => data
                        )

                    [2] => stdClass Object
                        (
                            [OutgoingMessageID] => data
                            [Reference] => data
                            [MessageNumber] => data
                            [PhoneNumber] => data
                            [Message] => data
                            [ReceivedDate] => data
                        )

                )

        )

)

如果只返回 1 个结果,我可以简单地执行以下操作:

$reference = $result->GetIncomingMessagesResult->SMSIncomingMessage->Reference;

那么我将如何处理多个结果呢?

任何帮助将不胜感激。

4

5 回答 5

41

它是一个数组,因此您可以使用以下命令轻松循环它foreach

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message) {
    echo $message->Reference;
}

然而值得注意的是SoapClient,默认情况下,PHP 似乎仅在数组中有多个值时才将数组作为 PHP 数组返回 - 如果只有一个值,您将只获得该值(不包含在数组中)。解决这个问题的一种简单方法是SOAP_SINGLE_ELEMENT_ARRAYSSoapClient构造函数中使用该选项;这将防止这种行为并确保您始终获得数组。

于 2009-06-04T14:18:29.413 回答
3

我的看法是始终确保你有一个消息数组,即使它是一个 1 的数组。这样你就不会重复任何处理。

$smsMessages = is_array( $result->GetIncomingMessagesResult->SMSIncomingMessage )
    ? $result->GetIncomingMessagesResult->SMSIncomingMessage
    : array( $result->GetIncomingMessagesResult->SMSIncomingMessage );

foreach ( $smsMessages as $smsMessage )
{
    echo $smsMessage->Reference;
}
于 2009-06-04T14:22:15.613 回答
2

您需要指定 SMSIncomingMessage 数组对象键。

$result->GetIncomingMessagesResult->SMSIncomingMessage[0]->Reference;

或者

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message)
{
$reference = $message[0]->Reference;
//...
}
于 2010-07-02T10:09:28.903 回答
1

将对象转换为数组

$array = (array) json_decode(['TEST'=>true]);
于 2019-11-29T02:20:42.797 回答
0

遍历数组?!:-)

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message)
{
    $reference = $message->Reference;
    //...
}
于 2009-06-04T14:19:17.057 回答