简体   繁体   English

无法从 smsgateway.me 循环受保护的对象

[英]Unable To Loop a Protected Object From smsgateway.me

I am actually using smsgateway.me to receive and send sms the problem is that the response from api is being returned as an object and the properties in it are all protected i actually wanted it to show all the messages i received in a table format using foreach loop here is an example response.我实际上是在使用 smsgateway.me 来接收和发送短信,问题是来自 api 的响应作为对象返回并且其中的属性都受到保护我实际上希望它显示我以表格格式收到的所有消息使用这里的 foreach 循环是一个示例响应。

SMSGatewayMe\Client\Model\MessageSearchResult Object
 (
[count:protected] => 59
[results:protected] => Array
    (
        [0] => SMSGatewayMe\Client\Model\Message Object
            (
                [id:protected] => 63384039
                [deviceId:protected] => 89747
                [message:protected] => test m 2
                [status:protected] => sent
                [log:protected] => Array
                    (
                        [0] => SMSGatewayMe\Client\Model\MessageLog Object
                            (
                                [status:protected] => failed
                                [occurredAt:protected] => DateTime Object
                                    (
                                        [date] => 2018-05-13 21:14:49.000000
                                        [timezone_type] => 1
                                        [timezone] => +00:00
                                    )
                            )
                    )
                [createdAt:protected] => DateTime Object
                    (
                        [date] => 2018-05-13 21:14:49.000000
                        [timezone_type] => 1
                        [timezone] => +00:00
                    )
                [updatedAt:protected] =>
            )
    )
)

Here is my code这是我的代码

    $config = Configuration::getDefaultConfiguration();
$config->setApiKey('Authorization', 'my-api-key');
$apiClient = new ApiClient($config);
$messageClient = new MessageApi($apiClient);


$messages = $messageClient->searchMessages(
    [
        'filters' => [
            [
                [
                    'field' => '103150',
                    'operator' => '=',
                    'value' => '1'
                ],
                [
                    'field' => 'status',
                    'operator' => '=',
                    'value' => 'received'
                ]
            ],

        ],
        'order_by' => [
            [


                'field'=> 'created_at',
                'direction'=> 'desc'


                // 'field' => 'status',
                // 'direction' => 'ASC'
            ],
        ],

    ]
);
       print_r($messages);

When you see objects with protected/private data, this is either information that is not 'usable' by a consumer, or something which should be available via the class methods.当您看到具有受保护/私有数据的对象时,这要么是消费者“不可用”的信息,要么是应该通过类方法可用的信息。

As far as I can tell from reading the source code of the API ...据我阅读API源代码可以看出......

$msgs = $messages->getResults();

will give you the value of [results:protected] , which is a list of Message objects.将为您提供[results:protected]的值,它是Message对象的列表。

You can then...那么你可以...

   foreach ( $msgs as $msg ) {
       echo $msg->getMessage().PHP_EOL;
   }

This is purely from reading the source code via Github and so may not be how it is intended to work.这纯粹是通过 Github 阅读源代码,因此可能不是它的预期工作方式。

Or to build a list...或者建立一个列表...

$msgOut = [];
foreach ( $msgs as $msg ) {
    $msgOut[] = ["msg" => $msg->getMessage()];  // Add other infor here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM