首页 > 解决方案 > php从对象中检索值

问题描述

我想从从 API 调用响应接收到的 PHP 对象中检索特定值,但是我无法让它工作。

对象(主体)采用以下格式:

object(stdClass)#29 (2) {
  ["success"]=>
  bool(true)
  ["result"]=>
  object(stdClass)#19 (1) {
    ["events"]=>
    array(1) {
      [0]=>
      object(stdClass)#4 (6) {
        ["id"]=>
        int(743)
        ["tzShift"]=>
        int(60)
        ["utcTime"]=>
        int(1551703477)
        ["upTime"]=>
        int(20953)
        ["event"]=>
        string(14) "DisplayTouched"
        ["params"]=>
        object(stdClass)#11 (4) {
          ["x"]=>
          int(46)
          ["y"]=>
          int(194)
          ["dx"]=>
          int(0)
          ["dy"]=>
          int(0)
        }
      }
    }
  }
}

我正在尝试通过以下方式检索值:

$body = json_decode($response->getBody());
$events = $body->result->events->params->x;

我究竟做错了什么?
谢谢

标签: phpapiobjectguzzle

解决方案


推荐阅读