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

时间:2018-11-01 19:51:30

标签: php

我实际上正在使用smsgateway.me来接收和发送短信,问题是来自api的响应作为对象返回,并且其中的属性都受到保护,我实际上希望它显示我在使用foreach循环的表格格式是一个示例响应。

import pygame, sys, random
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 600
windowHeight = 400
windowSize = (600, 400)
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
display = pygame.Surface((300, 200))
black = (0, 0, 0)
white = (225, 225, 255)
green = (0, 255, 0)
moveRight = False
moveLeft = False
moveSpeed = 6

level=   [['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','2','2','2','2','2','0','0','0','0','0','0','0'],
    ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
    ['2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2'],
    ['1','1','2','2','2','2','2','2','2','2','2','2','0','0','2','2','2','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1','1','1','1','1','0','0','1','1','1','1','1']]

grass = pygame.image.load('grass.png')
dirt = pygame.image.load('dirt.png')
player = pygame.image.load('player.png').convert()
playerRect = pygame.Rect(100, 100, 5, 13)
player.set_colorkey((255,255,255))

while True:
    display.fill((214, 42, 78))
    #display.blit(player,(playerRect.x,playerRect.y))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                moveRight = True
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                moveLeft = False
        if event.type == pygame.KEYUP:
            if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
                moveRight = False
            if event.type == pygame.K_LEFT or event.key == pygame.K_a:
                moveLeft = False
    display.blit(player,(playerRect.x,playerRect.y))



    playerMovement = [0, 0]
    if moveRight == True:
        playerMovement[0] += 2
    if moveLeft == True:
        playerMovement[0] -= 2


    tileRect = []
    y = 0
    for row in level:
        x = 0
        for col in row:
            if col == '2':
                display.blit(grass, (x*16, y*16))
            if col == '1':
                display.blit(dirt, (x*16, y*16))
            if col != '0':
                tileRect.append(pygame.Rect(x*16,y*16,16,16))
            x += 1
        y += 1


    #pygame.draw.rect(windowSurface, black, player)
    windowSurface.blit(pygame.transform.scale(display, windowSize),(0,0))
    pygame.display.update()
    mainClock.tick(60)

这是我的代码

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] =>
            )
    )
)

1 个答案:

答案 0 :(得分:0)

当您看到带有受保护/私有数据的对象时,这要么是消费者无法“使用”的信息,要么是可以通过类方法获得的信息。

据我所读的source code of the API ...

$msgs = $messages->getResults();

将为您提供[results:protected]的值,它是Message对象的列表。

然后您可以...

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

这纯粹是通过Github读取源代码,因此可能不是它的预期工作方式。

或者建立列表...

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