0

我正在使用 cURL 通过 API 访问数据。返回的结果是 JSON。我正在尝试访问部分返回的数据以向用户显示相关信息。这是返回的示例:

[{
    "FirstName": "Person1",
    "LastName": "Guy",
    "SISId": "7",
    "MiddleName": "",
    "EmailAddress": "demo@yahoo.com",
    "FederalId": "",
    "StateId": "",
    "ActiviationDate": "\/Date(1375751488440)\/",
    "ExpirationDate": null,
    "Status": 1,
    "UserId": 5632414,
    "MembershipId": 5632414,
    "Role": 5,
    "IsOnline": false,
    "HasUnreadMessages": false,
    "LastLoginDate": null,
    "LastActivityDate": null
}, {
    "FirstName": "Person2",
    "LastName": "Girl",
    "SISId": "18",
    "MiddleName": "Bailey",
    "EmailAddress": "superstar08_03@yahoo.com",
    "FederalId": "",
    "StateId": "",
    "ActiviationDate": "\/Date(1376928013777)\/",
    "ExpirationDate": null,
    "Status": 1,
    "UserId": 5932535,
    "MembershipId": 5932535,
    "Role": 5,
    "IsOnline": false,
    "HasUnreadMessages": false,
    "LastLoginDate": null,
    "LastActivityDate": null
}]

我尝试了几种不同的方法使用 PHP 来尝试访问UserId数据,但都没有成功。这是我最新的代码:

<?php
//API CALL IS MADE ABOVE THIS LINE
$ch = curl_init();

curl_setopt_array($ch, $options);

$output = curl_exec($ch);

$info = curl_getinfo($ch);

if ($output === false || $info['http_code'] != 200) {
       $output = "No cURL data returned [" . $info['http_code'] . "]";
       if (curl_error($ch)) {
              $output .= "\n" . curl_error($ch);
       }
}

$object = json_decode($output);

print $obj->{'FirstName'}; // Person1
?>
4

1 回答 1

1

$object是一个数组。

如果你想要一个人:

echo $object[0]->FirstName;
于 2013-10-03T21:27:03.933 回答