Reputation: 11
after the youtube api v3 update I don't know how to repair the video section of my site. The section shows videos uploaded in my youtube channel UCBL3ecWukz_EX7FrDmtdgJQ
I already created OAuth: Client ID for web application ,Public API access: Key for server applications and Key for browser applications.
Now I'm trying to understand how to use them to retrieve a list of the videos uploaded on my channel.
I tried this code
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCBL3ecWukz_EX7FrDmtdgJQ&maxResults=20&key=***************************************
now I don't know what to do with the output.. something like that
{
"kind": "youtube#searchListResponse",
"etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/6M9Lo8BzT3ingng1jJF2bBh1CNs\"",
"pageInfo": {
"totalResults": 5,
"resultsPerPage": 20
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/LHUK9eLdq9H2-Szizgc5BvV-gxM\"",
"id": {
"kind": "youtube#channel",
"channelId": "UCBL3ecWukz_EX7FrDmtdgJQ"
},
"snippet": {
"publishedAt": "2014-12-26T11:20:46.000Z",
"channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
"title": "Parrocchia Sant'Agata Alì",
"description": "",
"thumbnails": {
"default": {
"url": "https://lh5.googleusercontent.com/-O78yDecTsmk/AAAAAAAAAAI/AAAAAAAAAAA/U-NQ4NkzZQE/photo.jpg"
},
"medium": {
"url": "https://lh5.googleusercontent.com/-O78yDecTsmk/AAAAAAAAAAI/AAAAAAAAAAA/U-NQ4NkzZQE/photo.jpg"
},
"high": {
"url": "https://lh5.googleusercontent.com/-O78yDecTsmk/AAAAAAAAAAI/AAAAAAAAAAA/U-NQ4NkzZQE/photo.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/YPX-4TZwL2BMwOoIP3pO2_KBzvs\"",
"id": {
"kind": "youtube#video",
"videoId": "FjGA6cjh5zI"
},
"snippet": {
"publishedAt": "2015-02-12T23:45:39.000Z",
"channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
"title": "Alì S Agata Bacio del braccio",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/FjGA6cjh5zI/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/FjGA6cjh5zI/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/FjGA6cjh5zI/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/-m38lEymXlK8ZVMm2dNRzX8e8ro\"",
"id": {
"kind": "youtube#video",
"videoId": "7PQBjOedbtk"
},
"snippet": {
"publishedAt": "2015-02-07T20:43:40.000Z",
"channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
"title": "Annuncio Festa Grande",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/7PQBjOedbtk/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/7PQBjOedbtk/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/7PQBjOedbtk/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/Bv1fyl1c4aVHIrWuOV5gNf7xpMI\"",
"id": {
"kind": "youtube#video",
"videoId": "Gmelq3hGEPI"
},
"snippet": {
"publishedAt": "2015-02-15T23:17:22.000Z",
"channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
"title": "Alì S Agata riposta sull'altare",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/Gmelq3hGEPI/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/Gmelq3hGEPI/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/Gmelq3hGEPI/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/LZFjbm4TuWdxvls0Jzan6H6Ow5A\"",
"id": {
"kind": "youtube#video",
"videoId": "GMizgWpyt2k"
},
"snippet": {
"publishedAt": "2015-03-27T22:13:32.000Z",
"channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
"title": "Alì Chiesa S Agata Preghiera per devozione Padre Serafino",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/GMizgWpyt2k/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/GMizgWpyt2k/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/GMizgWpyt2k/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
}
]
}
could someone explain me, please Really Thank you!! ps. sorry for the second huge code quote
Upvotes: 1
Views: 2117
Reputation: 598
What a coincidence... I just needed this a few days ago :) Here's the code I used:
<?php
$playlistID = ''; // The Playlist ID for your 'uploads'-playlist.
$videofile = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=10&playlistId=' . $playlistID;
$json = file_get_contents($videofile);
$json = preg_replace('/^\xEF\xBB\xBF/', '', $json);
$array = json_decode($json, TRUE);
foreach ($array as $key => $value) {
if ($key == "items") {
echo "<table class='vtable'>";
for ($i = 0; $i < count($array["items"]); $i = $i + 1) {
$id = $array["items"][$i]['snippet']['resourceId']['videoId'];
$thumbnail = $array["items"][$i]['snippet']['thumbnails']['high']['url'];
$title = $array["items"][$i]['snippet']['title'];
$description = $array["items"][$i]['snippet']['description'];
// do some stuff with the variables
}
}
}
?>
Upvotes: 2
Reputation: 355
1) Make a HTTP request from PHP to get the JSON data as a string. Do this using cURL, a library like Guzzle, or a built-in function like file_get_contents(). Reference: http://php.net/manual/en/book.curl.php
$youtube_api_string = file_get_contents("http://your_api_url");
2) Convert that string into a standard object using
$api_result = json_decode($youtube_api_string);
Reference: http://php.net/manual/en/function.json-decode.php
3) Write a loop to assemble your search results into usable YouTube URLs
$youtube_urls = [];
for ($i = 0; $i < count($api_result->items); $i++) {
$video = $api_result->items[$i];
array_push($youtube_urls, ["url" => "http://youtube.com/watch?v=" . $video->id->videoId, "title" => $video->snippet->title ]);
}
From here, you should be able to loop through the $youtube_urls
variable to generate the list of videos on your channel on your site.
Upvotes: 0