Gaiscioch Select Chapter
POPULAR ADVENTURES:



ACTIVE ADVENTURES:





ADVENTURES:
Palia
Blood of Steel
Baldur's Gate 3
Dune Awakening
Elder Scrolls Online
Conan Exiles
Albion Online
Deep Rock Galactic
Valheim
Foxhole
Enshrouded
- Full List -
CHAPTERS:
Chapter 8:
Conqueror's Blade (2019)
Chapter 7:
New World (2021)
Chapter 6:
World of Warcraft: Classic (2019)
Chapter 5:
Elder Scrolls Online (2014)
Chapter 4:
Guild Wars 2 (2012)
Chapter 3:
RIFT (2011)
Chapter 2:
Warhammer Online (2008)
Chapter 1:
Dark Age of Camelot (2001)
Community
Events
CHARITY:

LEGACY EVENTS:


Search Gaiscioch.com:
137 Tuatha Guilds:
8,407 Members:
14,061 Characters:
11,709 Items:
  • Views: 3,126
  • Replies: 3

Need Help: PHP / JSON / YouTube API

Taoiseach de na Arach Glas
Foghladha
Taoiseach de na Arach Glas
  • GW2: Foghladha.2506
  • ESO: @Foley
Posted On: 09/12/2015 at 04:05 PM
  • Steam
  • PSN
  • XBOX
  • Twitch
  • Twitch
  • Twitter

Hey Everyone,

I know quite a few of you are web junkies and probably know the new oAuth2 and V3 Youtube API better than I do. I could use some help creating a simple function that will turn a Playlist ID into a json array width data so that I can import it into a DB.

function importPlaylist($playlistID) {

   ... Code ...

   return $jsonarray;

}

If you can help with this please let me know. I've been battling this on my free time and If I could just see 1 working example it would go a long way to helping me understand how it works. I have API Keys to input I just need the code to allow me to do it.

Looking to import: https://www.youtube.com/playlist?list=PLWRYhlET-aA4OKp4w6qb-YBQXRvbYOXWi

This used to be easy, not so much anymore.

"It's not the loot and accolades you walk away with, it's the memories and friendships that you cherish forever." - Foghladha
Awards & Achievements
Devotion Rank 20Valor Rank 20Fellowship Rank 10Explorer Rank 12Scholar Rank 15Artisan Rank 8Social Rank 9Mentorship Rank 6

Response:

Laoch de na Iolair Buí
Sekkerhund
Laoch de na Iolair Buí
  • GW2: Sekkerhund.3790
  • ESO: @Sekkerhund
Replied On: 09/12/2015 at 08:08 PM PDT
  • Steam
  • PSN
  • XBOX
  • Twitch
  • Twitter

Have you looked @ https://developers.google.com/youtube/v3/docs/playlistItems/list  (Examples section >  click PHP)?  I honestly have no idea if this will help you or not, but the STE in me just can't resist debugging a problem.

There's actually a working test at the very bottom, too.  I was able to pull the data from your linked playlist using part=snippet and playlistId=PLWRYhlET-aA4OKp4w6qb-YBQXRvbYOXWi

The code sample below calls the API's playlistItems.list method to retrieve a list of videos uploaded to the channel associated with the request. The code also calls the channels.list method with the mine parameter set to true to retrieve the playlist ID that identifies the channel's uploaded videos.

This example uses the PHP client library.

<?php

// Call set_include_path() as needed to point to your client library.
require_once
'Google/Client.php';
require_once
'Google/Service/YouTube.php';
session_start
();

/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */

$OAUTH2_CLIENT_ID
= 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET
= 'REPLACE_ME';

$client
= new Google_Client();
$client
->setClientId($OAUTH2_CLIENT_ID);
$client
->setClientSecret($OAUTH2_CLIENT_SECRET);
$client
->setScopes('https://www.googleapis.com/auth/youtube');
$redirect
= filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
  FILTER_SANITIZE_URL
);
$client
->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube
= new Google_Service_YouTube($client);

if (isset($_GET['code'])) {
 
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
   
die('The session state did not match.');
 
}

  $client
->authenticate($_GET['code']);
  $_SESSION
['token'] = $client->getAccessToken();
  header
('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client
->setAccessToken($_SESSION['token']);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
 
try {
   
// Call the channels.list method to retrieve information about the
   
// currently authenticated user's channel.
    $channelsResponse
= $youtube->channels->listChannels('contentDetails', array(
     
'mine' => 'true',
   
));

    $htmlBody
= '';
   
foreach ($channelsResponse['items'] as $channel) {
     
// Extract the unique playlist ID that identifies the list of videos
     
// uploaded to the channel, and then call the playlistItems.list method
     
// to retrieve that list.
      $uploadsListId
= $channel['contentDetails']['relatedPlaylists']['uploads'];

      $playlistItemsResponse
= $youtube->playlistItems->listPlaylistItems('snippet', array(
       
'playlistId' => $uploadsListId,
       
'maxResults' => 50
     
));

      $htmlBody
.= "<h3>Videos in list $uploadsListId</h3><ul>";
     
foreach ($playlistItemsResponse['items'] as $playlistItem) {
        $htmlBody
.= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'],
          $playlistItem
['snippet']['resourceId']['videoId']);
     
}
      $htmlBody
.= '</ul>';
   
}
 
} catch (Google_Service_Exception $e) {
    $htmlBody
.= sprintf('<p>A service error occurred: <code>%s</code></p>',
      htmlspecialchars
($e->getMessage()));
 
} catch (Google_Exception $e) {
    $htmlBody
.= sprintf('<p>An client error occurred: <code>%s</code></p>',
      htmlspecialchars
($e->getMessage()));
 
}

  $_SESSION
['token'] = $client->getAccessToken();
} else {
  $state
= mt_rand();
  $client
->setState($state);
  $_SESSION
['state'] = $state;

  $authUrl
= $client->createAuthUrl();
  $htmlBody
= <<<END
 
<h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl">authorize access</
a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
 
<head>
   
<title>My Uploads</title>
 
</head>
 
<body>
   
<?=$htmlBody?>
 
</body>
</html>

So, when I plug part=snippet and playlistId=PLWRYhlET-aA4OKp4w6qb-YBQXRvbYOXWi  into the Test section:

Response

200 OK
- Show headers -
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"jOXstHOM20qemPbHbyzf7ztZ7rI/D3G7y_ZyUpWi54pFyRDXR18VzfU\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 38,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"jOXstHOM20qemPbHbyzf7ztZ7rI/oA9WUa0qWzOCjDswMt54rr9HxX0\"",
"id": "PL74IkRAmQ4TbhnUUAHy5ERFVuB9SX2yoB9yeUf0WXF0U",
"snippet": {
"publishedAt": "2015-09-12T05:04:14.000Z",
"channelId": "UCAu76gks3UK92w3mR2WoBlw",
"title": "Deleted video",
"description": "This video is unavailable.",
"channelTitle": "Gaiscioch",
"playlistId": "PLWRYhlET-aA4OKp4w6qb-YBQXRvbYOXWi",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "vgi5CYCG29Y"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"jOXstHOM20qemPbHbyzf7ztZ7rI/6nXxhB96FTcSyh6MXpR10BjxjYw\"",
"id": "PL74IkRAmQ4TbhnUUAHy5ERJ6wLAYflkBWYwh-zcJGBRk",
"snippet": {
"publishedAt": "2015-09-11T07:47:37.000Z",
"channelId": "UCAu76gks3UK92w3mR2WoBlw",
"title": "Lets Play Mount & Blade Warband: Viking Conquest of Fog's Side Quests @TaleWorlds #Mount&Blade",
"description": "In a land torn asunder by incessant warfare, it is time to assemble your own band of hardened warriors and enter the fray. Lead your men into battle, expand your realm, and claim the ultimate prize: the throne of Calradia!\n\nJoin Foghladha in his first Mount & Blade experience! Prepare to laugh your arse off! Yes people he really does suck this bad!",
"thumbnails": {
"default": {
"width": 120,
"height": 90
},
"medium": {
"width": 320,
"height": 180
},
"high": {
"width": 480,
"height": 360
},
"standard": {
"width": 640,
"height": 480
},
"maxres": {
"width": 1280,
"height": 720
}
},
"channelTitle": "Gaiscioch",
"playlistId": "PLWRYhlET-aA4OKp4w6qb-YBQXRvbYOXWi",
"position": 1,
"resourceId": {
"kind": "youtube#video",
"videoId": "D4gnJ9pskrw"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"jOXstHOM20qemPbHbyzf7ztZ7rI/b1Ch5EyXL_dg0CB6pEJJUTwErkw\"",
"id": "PL74IkRAmQ4TbhnUUAHy5EROp9F9X1kNn6I7OyQloQ6no",
"snippet": {
"publishedAt": "2015-09-11T05:36:26.000Z",
"channelId": "UCAu76gks3UK92w3mR2WoBlw",
"title": "First Look! Otherland on Fog's Side Quests #Otherlands #FirstLook #Preview",
"description": "Foghladha takes a first look at Otherland, an action-packed Massively Multiplayer Online Role Playing Game based on Tad Williams’s highly acclaimed novels. Embark on exciting adventures in a virtual multiverse, with worlds ranging from fantasy to sci-fi, and so much more.",
"thumbnails": {
"default": {
"width": 120,
"height": 90
},
"medium": {
"width": 320,
"height": 180
},
"high": {
"width": 480,
"height": 360
},
"standard": {
"width": 640,
"height": 480
},
"maxres": {
"width": 1280,
"height": 720
}
},
"channelTitle": "Gaiscioch",
"playlistId": "PLWRYhlET-aA4OKp4w6qb-YBQXRvbYOXWi",
"position": 2,
"resourceId": {
"kind": "youtube#video",
"videoId": "O-4Z13_gbek"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"jOXstHOM20qemPbHbyzf7ztZ7rI/wSnMVRf8Toq5-Ak_78-vBssWiNM\"",
"id": "PL74IkRAmQ4TbhnUUAHy5ERAh2bBT2VGtUVI4_ZNDIsfg",
"snippet": {
"publishedAt": "2015-09-09T07:53:32.000Z",
"channelId": "UCAu76gks3UK92w3mR2WoBlw",
"title": "Lets Play Allods Online on Fog's Side Quests - @AllodsTeam #Allods",
"description": "Foghladha journeys into Allods Online in preparation for a future Stream.",
"thumbnails": {
"default": {
"width": 120,
"height": 90
},
"medium": {
"width": 320,
"height": 180
},
"high": {
"width": 480,
"height": 360
},
"standard": {
"width": 640,
"height": 480
},
"maxres": {
"width": 1280,
"height": 720
}
},
"channelTitle": "Gaiscioch",
"playlistId": "PLWRYhlET-aA4OKp4w6qb-YBQXRvbYOXWi",
"position": 3,
"resourceId": {
"kind": "youtube#video",
"videoId": "tpl65NxLfXo"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"jOXstHOM20qemPbHbyzf7ztZ7rI/VAYW6PS0yMBwiV0Yx41QG44Mayc\"",
"id": "PL74IkRAmQ4TbhnUUAHy5ERJkzKxZgWJbGxBbcRMQ5ya8",
"snippet": {
"publishedAt": "2015-09-07T08:39:56.000Z",
"channelId": "UCAu76gks3UK92w3mR2WoBlw",
"title": "Let's Play Rainbow Six: Vegas 2 on Fog's Side Quests #LetsPlay #RainbowSix @YouTubeGaming",
"description": "Donaliam and Foghladha take on Vegas in Tom Clancy's Rainbow Six Vegas 2 on Fog's Side Quests",
"thumbnails": {
"default": {
"width": 120,
"height": 90
},
"medium": {
"width": 320,
"height": 180
},
"high": {
"width": 480,
"height": 360
},
"standard": {
"width": 640,
"height": 480
},
"maxres": {
"width": 1280,
"height": 720
}
},
"channelTitle": "Gaiscioch",
"playlistId": "PLWRYhlET-aA4OKp4w6qb-YBQXRvbYOXWi",
"position": 4,
"resourceId": {
"kind": "youtube#video",
"videoId": "sauZ8k2qdNc"
}
}
}
]
}

 

Seems to me that there's data in there that you're needing to convert into a json array, but if that conversion process is your actual issue, then I gots no idea.

Awards & Achievements
Devotion Rank 20Valor Rank 10Fellowship Rank 20Scholar Rank 9Artisan Rank 6Social Rank 7
Taoiseach de na Arach Glas
Foghladha
Taoiseach de na Arach Glas
  • GW2: Foghladha.2506
  • ESO: @Foley
Replied On: 09/12/2015 at 09:04 PM PDT
  • Steam
  • PSN
  • XBOX
  • Twitch
  • Twitch
  • Twitter

Yeah, ive read through that page and have not gotten it to wprk on our server. I downnloaded the git and i get an error when i try to use it.

"It's not the loot and accolades you walk away with, it's the memories and friendships that you cherish forever." - Foghladha
Awards & Achievements
Devotion Rank 20Valor Rank 20Fellowship Rank 10Explorer Rank 12Scholar Rank 15Artisan Rank 8Social Rank 9Mentorship Rank 6
Taoiseach de na Arach Glas
Foghladha
Taoiseach de na Arach Glas
  • GW2: Foghladha.2506
  • ESO: @Foley
Replied On: 09/12/2015 at 09:05 PM PDT
  • Steam
  • PSN
  • XBOX
  • Twitch
  • Twitch
  • Twitter

That and im looking for something a bit less bulky

"It's not the loot and accolades you walk away with, it's the memories and friendships that you cherish forever." - Foghladha
Awards & Achievements
Devotion Rank 20Valor Rank 20Fellowship Rank 10Explorer Rank 12Scholar Rank 15Artisan Rank 8Social Rank 9Mentorship Rank 6
[0.1594]