PHP无法从json编码读取对象
我正在向youtube API发送请求以获取播放列表:PHP无法从json编码读取对象,php,json,wordpress,Php,Json,Wordpress,我正在向youtube API发送请求以获取播放列表: // make GET request to the YouTube API $response = wp_remote_request( 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.$playlist_id.'&key='.$api_key.'&order=date&ma
// make GET request to the YouTube API
$response = wp_remote_request( 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.$playlist_id.'&key='.$api_key.'&order=date&maxResults=25',
array(
'method' => 'GET'
)
);
得到它后,我希望它是JSON格式的,因此我使用:
$json = wp_remote_retrieve_body($response);
$json_encoded = json_encode($json, true);
如果我是对的,那么现在应该是所提供值的JSON表示。因此,我得到:
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"p4VTdlkQv3HQeTEaXevLePAydmU/Q2DmelXR9Ne48evrmJE9Q4yVvpU\"",
"nextPageToken": "CAoQAE",
"pageInfo": {
"totalResults": 36,
"resultsPerPage": 25
},
"items": [ ...
我只想储存这些物品。人们会认为我现在可以通过以下方式访问它们:
$json_encoded['items'];
然后将其存储到WordPress数据库中,如下所示:
// if we have any items update the db with those results
if ( isset ( $json_encoded['items'] )) {
$videos['json'] = $json_encoded['items'];
update_option( 'wpc_ylp_videos', $videos );
}
很不幸,我得到了以下错误:
Warning: Illegal string offset 'items' in /Users/tobias/Sites/videos.dev.cc/wp-content/plugins/videos/includes/videos.php on line 61
"
及
我肯定我做错了什么事,但我不能对此束手无策。如何访问items对象?我对json_encode的使用正确吗?对于那些不阅读评论的人;答案是使用json_解码而不是json_编码 json_encode转换为字符串
json_encode转换为字符串。我相信你正在寻找json_decode Ty Bailey,谢谢,这正是问题所在!哦,天哪。
Notice: Undefined index: json in /Users/tobias/Sites/videos.dev.cc/wp-content/plugins/videos/includes/videos.php on line 67