我是php新手。我有以下情况:
$params = array("code" => $_GET["code"], "redirect_uri" => $redirectUrl);
$response = $client->getAccessToken($accessTokenUrl, "authorization_code", $params);
$accessTokenResult = $response["result"];
$client->setAccessToken($accessTokenResult["access_token"]);
$client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER);
$response = $client->fetch("https://oauth.reddit.com/api/v1/me.json");
print_r
的结果($response
)就是这个:
Array (
[result] => Array
(
[name] => histograf
[created] => 12869553489
[gold_creddits] => 0
[created_utc] => 12869553489
[link_karma] => 1
[comment_karma] => 11
[over_18] => 1
[is_gold] =>
[is_mod] => 0
[has_verified_email] =>
[id] => ap11l
)
[code] => 200
[content_type] => application/json; charset=UTF-8 )
现在重点是,如何访问元素名称?我需要在pgm中继续使用用户名。
我已尝试过这些但没有工作:
$username = $response[0];
$username = $response->name;
$username = $response[0]name;
答案 0 :(得分:1)
解决方案:
$username = $response['result']['name'];
答案 1 :(得分:0)
在您的示例中,要从响应中获取[name]字段:
$username = $response['result']['name'];
将['name']
替换为您尝试访问的任何字段。为了将来参考,如果您要在HTML文件中测试脚本的输出,则可以将print_r()
标记为<pre>
标记:
<pre><?php print_r($response); ?></pre>
这使print_r()
更具可读性,您将能够看到数组的深度。