使用PHP的asana getUserInfo错误

时间:2014-04-28 08:22:01

标签: php

这是我的代码

<?php 
require_once('asana.php');

$asana = new Asana(array('apiKey' => 'XXXXXXXX')); // API Key

$userinfo=$asana->getUserInfo();

if ($asana->responseCode != '200' || is_null($userinfo)) {
    echo 'Error while trying to connect to Asana, response code: ' . $asana->responseCode;
    return;
}
 $resultJson = json_decode($userinfo);

foreach ($resultJson->data as $user) {
    echo  $user->name . ' (id ' . $user->id . ')' .  PHP_EOL;
}

?>

在asana.php中

public function getUserInfo($userId = null, array $opts = array()) {
        $options = http_build_query($opts);

        if (is_null($userId)) {
            $userId = 'me';
        }

        return $this->askAsana($this->userUrl . '/' . $userId . '?' . $options);
    }

我得到这样的错误:注意:尝试在第15行的C:\ wamp \ www \ pngtest \ index.php中获取非对象的属性

有什么问题?提前致谢

1 个答案:

答案 0 :(得分:1)

$resultJson = json_decode($userinfo);

foreach ($resultJson->data as $user) {
}

在您知道$resultJson->data是否为有效对象之前,您是否尝试访问$resultJson

请注意json_decode返回:

  

如果无法解码json或者编码数据的深度超过递归限制,则返回NULL。

$resultJson可能(很可能)为NULL,因为它被赋予了无效的JSON数据。您应该echo $userinfo并确定它是否符合您的期望。