- json_decode: documentation ( source)
- array_keys: documentation ( source)
- sprintf: documentation ( source)
<?php
$username = 'UU3dESJmRgwl8j3X80c9wTwg';
// YouTube API URL retrieves the fields we want in JSON format so that it's super easy to work with
$api_url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=%s&key=AIzaSyAMUb-pd1gAIeNmu3nUrarg6_397QJQXEo';
// Get the videos for our user with 'wp_remote_get()'
$response = http_get( sprintf($api_url, $username) , array("timeout"=>1), $info );
echo(response);
echo($info);
// Let's decode what we retrieved so that we can store it efficiently
$item_data = json_decode( $info );
// If our info isn't in an array then these next steps won't work-- return false
if ( !is_array( $item_data ) ) {
return false;
}
// Let's sort through everything we've got. We'll keep the stuff we need and drop the rest from the array.
$item_data = $item_data['feed']['entry'];
foreach(array_keys($item_data) as $key) {
echo($item_data[$key]['title']['$t']);
}