我正在向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&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
"和
Notice: Undefined index: json in /Users/tobias/Sites/videos.dev.cc/wp-content/plugins/videos/includes/videos.php on line 67我确信我做错了什么,但我不能理解这一点。如何访问items对象?我的json_encode用法正确吗?
发布于 2019-12-20 04:03:03
对于那些不阅读评论的人来说,答案是使用json_decode()而不是json_encode()。
json_encode()转换为字符串。
https://stackoverflow.com/questions/59414553
复制相似问题