首页 > 解决方案 > 如何从 API 获取特定信息到变量中?

问题描述

我有一些来自 API 的输出:

{"id":"usr_3290ad77-a2d8-40da-9edf-21e624c23f27","username":"knuffelbeestje","displayName":"KnuffelBeestje","bio":"Working on a new worldǃ\nDC Hub is coming soon to community labsǃ","bioLinks":["https://steamcommunity.com/id/KnuffelBeestje","https://twitch.tv/knuffeldiertje"],"currentAvatarImageUrl":"https://api.vrchat.cloud/api/1/file/file_d66e4e65-6ce7-4321-8770-1c70840adfa9/1/file","currentAvatarThumbnailImageUrl":"https://api.vrchat.cloud/api/1/image/file_d66e4e65-6ce7-4321-8770-1c70840adfa9/1/256","tags":["show_social_rank","system_trust_basic","system_avatar_access","system_world_access","language_nld","language_eng"],"developerType":"none","last_login":"","last_platform":"standalonewindows","allowAvatarCopying":false,"isFriend":false,"friendKey":"","location":"","worldId":"offline","instanceId":"offline"}int(1) 1

我是如何得到它的:

$curl = curl_init('https://api.vrchat.cloud/api/1/users/KnuffelBeestje/name? 
apiKey=JlE5Jldo5Jibnk5O5hTx6XVqsJu4WJ26');
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Authorization: Basic PrivateKey']);
$apicontent = curl_exec($curl);
curl_close($curl);

var_dump(json_decode($apicontent, true));

(API 密钥是公开的,不用担心)。我想将“bio”保存到 PHP 中的变量中,我该怎么做?

标签: phpcurl

解决方案


json_decode函数返回一个关联数组。这意味着您可以附加['bio']到它并获取 bio 的值(当然也可以是任何其他值)。然后只需将其保存在一个变量中

$bio = json_decode($apicontent, true)['bio'];


推荐阅读