Json API -> 尝试获取非对象的 属性

Json API -> Trying to get property of non-object

我已经阅读了很多关于此的不同文章,但似乎无法找到有效的解决方案。

我目前正在努力从 API 中获取 json 数据(我必须先授权连接)并从中输出数据。 (只是点点滴滴,比如name, id等,不是全部)

首先,让我向您展示我的代码:

class Connector {
//authorization to the API
public $token;
private $username = 'myuser';
private $password = 'mypassword';

public function __construct() {
    $this->getToken();
    setcookie ('token', $this->token, time() + 3600, 'https://mycloud.com');
}

public function getToken() {
    $url = 'https://auth.mycloud.ch/sso2.php';

    $fields = array(
        'username' => $this->username,
        'password' => $this->password
    );
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );

    $result = curl_exec($ch);

    $this->token = json_decode($result, true)['token'];

    curl_close($ch);
}


public function getData($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_COOKIE, 'token=' . $this->token);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($ch); 

    curl_close($ch);

    echo $result->{'id'};

    //print_r($result);


}
}


$cnt = new Connector();
$cnt->getData('https://hr.mycloud.ch/services/api/employee/10');

由于某些原因,当我执行 echo $result->{'id'} 时它无法输出数据;
它告诉我"Trying to get property of non-object"
但我已经在上面使用了json_decode...
$this->token = json_decode($result, true)['token'];

当我做 print_r($result);它给了我这个数组:

 [{"id":"10","name":"name","employee_id":"id@test.com","user_settings_id":"0","location_id":"1","company_id":"1","active":"1","employee_type":"type_ch"}] 


我到底做错了什么?我也用 foreach 循环试过,但它给了我同样的错误。
我希望你能帮助我,谢谢。

因为它是一个对象数组

$res = json_decode($result);

     foreach($res as $item){
        echo $item->{'id'};
     }

$res[0]->id;

你没有在你的 returned 字符串上使用 json_decode 从 curl 并且你没有得到一个对象,你得到一个对象数组,使用:

<?php
public function getData($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_COOKIE, 'token=' . $this->token);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($ch);
    $res = json_decode($result);

    curl_close($ch);

    // sidenote: Always check, if json_decode was successful, it can return false on failure
    if ( $res !== null ) {
        // you get an array of objects, not an object, see your json input
        echo $result[0]->{'id'};
    }

    //print_r($result);
}

注意:您应该检查 json_decode returns null 是否存在(失败时 return 值为 null)。