Youtube Api to get video description with video id - php

I am trying to get discriptiobn from youtube using api .... but i dont no where am i wrong its not getting for single video . but if i try playlist its working fine, not with video id
Here is my code
error_reporting(E_ALL);
$feedURL = 'https://gdata.youtube.com/feeds/api/playlists/'.$id.'?v=2&prettyprint=true';
$sxml = simplexml_load_file($feedURL);
echo $feedURL.'</br>';
foreach ($sxml->entry as $entry)
{
echo $media->group->description;
}
Above code is working with playlist ... but if i try one video its not working:
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$id.'?v=2&alt=json&prettyprint=true';
$sxml = simplexml_load_file($feedURL);
echo $feedURL.'</br>';
foreach ($sxml->entry as $entry)
{
echo $media->group->description;
}

If you call YouTube api with 'alt=json' parameter, the response will be formatted as JSON not XML. So you should use:
$response = json_decode(file_get_contents('... API URL ...'), true);
And then 'description' can be accessed with:
$response['entry']['media$group']['media$description']['$t']

Related

Displaying playlist data from YouTube API v3

I am trying to use the YouTube v3 API to show all the videos from a particular YouTube playlist. Here is my code so far:
<?php
$playlistId = 'PLHGPqm6HHcedxhoMXo9m9yKqsOmOB_S0F';
$maxResults = 20;
$apiKey = 'APIKEYHERE';
$string = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.$playlistId.'&maxResults='.$maxResults.'&key='.$apiKey.''));
foreach($string['items'] as $item) {
echo $item['title'];
}
?>
I have used Postman to test the url and it is definitely getting data but when i try to echo any data it isn't showing.
I have learnt that Json decode will return stdclass by default json_decode php.net so you would have to replace foreach with this:
foreach($string->items as $item) {
echo $item->snippet->title;
}

Instagram JSON avatar extraction PHP

I want to extract the profile_picture of any instagram user from the JSON url: https://www.instagram.com/just/media/ with PHP and print it out. I've tried:
$instaJSONUrl = 'https://www.instagram.com/just/media/';
$json = file_get_contents($instaJSONUrl);
$json_data = json_decode($json, true);
echo $json_data["profile_picture"];
Not working.. If I print out the $json only, I get:
{status":"ok","items":[],"more_available":false}
Meaning that the problem is with reading the URL. Does this mean I need access token, because I don't want to use one, as I already can get the profile image URL when I open https://www.instagram.com/just/media/ from my browser.
This will result in an array of users and their picture profile links.
<?php
$instaJSONUrl = 'https://www.instagram.com/just/media/';
$json = file_get_contents($instaJSONUrl);
$json_data = json_decode($json, true);
$users = array();
foreach($json_data['items'] as $item)
foreach($item['comments'] as $comment)
foreach($comment as $profile)
$users[$profile['from']['username']] = $profile['from']['profile_picture'];
print_r($users);
Managed to do it via accessing the meta tags directly, and specifically the <meta property="og:image" tag, with the following:
libxml_use_internal_errors(true);
$c = file_get_contents($instaURL);
$d = new DomDocument();
$d->loadHTML($c);
$xp = new domxpath($d);
and then I can print out the image url with:
foreach ($xp->query("//meta[#property='og:image']") as $el) {
echo $el->getAttribute("content");
}
You can even use it to access other properties, such as og:url and og:description for the specific Instagram user.
This is for all those people who needs to access & get Instagram user data (basic: profile_image, profile_url, profile_description, etc.), without dealing with Instagram's API.

Getting title, description and number of views of a youtube video

$youtube = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2');
$title = $youtube->title;
This gets the title. But how could I get the viewcount and description? tried $youtube->description; and $youtube->views;
I suggest you to use the JSON output instead of the XML one.
You can get it by adding the alt=json parameter to your URL:
http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json
Then you have to load the json and parse it:
<?php
$json_output = file_get_contents("http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json");
$json = json_decode($json_output, true);
//This gives you the video description
$video_description = $json['entry']['media$group']['media$description']['$t'];
//This gives you the video views count
$view_count = $json['entry']['yt$statistics']['viewCount'];
//This gives you the video title
$video_title = $json['entry']['title']['$t'];
?>
Hope this helps.
UPDATE
To see what variables does the JSON output have, add the prettyprint=true parameter to the URL and open it in your browser, it will beautify the JSON output to make it more comprehensible:
http://gdata.youtube.com/feeds/api/videos/wGG543FeHOE?v=2&alt=json&prettyprint=true
Instead of browsing the URL you can just write
echo "<pre>";
print_r($json);
echo "</pre>";
after
$json = json_decode($json_output, true);
and it will print the formatted JSON output
My code:
$vId = "Jer8XjMrUB4";
$gkey = "AIzaSyCO5lIc_Jlrey0aroqf1cHXVF1MUXLNuR0";
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=".$vId."&key=".$gkey."");
$data = json_decode($dur, true);
foreach ($data['items'] as $rowdata) {
$vTime = $rowdata['contentDetails']['duration'];
$desc = $rowdata['snippet']['description'];
}
$interval = new DateInterval($vTime);
$vsec = $interval->h * 3600 + $interval->i * 60 + $interval->s;
if($vsec > 3600)
$vsec = gmdate("H:i:s", $vsec);
else
$vsec = gmdate("i:s", $vsec);
echo $vsec."--".$desc;
Result:
02:47--Following the critically acclaimed global smash hit X-Men:
Youtube API V2.0 has been deprecated.You must have to switch to V3 API.Which need API key.
So prefer to use but In this way so can not get video description
http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=ktt3C7nQbbA&format=json

Get all my videos (including private ones) using OAuth2

I currently have something working where it gets all of my videos based on my Youtube username. Now I need to have it pull my private videos as well.
How I need everything to work is as I upload a video to Youtube it automatically embeds on my webpage which is password protected.
I created my API key and also used the Oauth 2.0 Sandbox and was able to retrieve all of the videos, including private but I do not know how to integrate the Oauth with my current coding which is below:
<?php
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/users/tprestinario/uploads';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>
<h1><?php echo $sxml->title; ?></h1>
<?php
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
// get <yt:duration> node for video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = $attrs['seconds'];
$videoid = $yt->videoid[0];
?>
<div class="item">
<h1><?php echo $media->group->title; ?></h1>
<p><?php echo $media->group->description; ?></p>
<iframe width="560" height="315" src="http://www.youtube.com/embed/<?php echo $videoid; ?>" frameborder="0" allowfullscreen></iframe>
</div>
<?php
}
?>
If you use YouTube Data API V3 that would be much easier.
Here's a nice example for this use case.
"https://code.google.com/p/youtube-api-samples/source/browse/samples/php/my_uploads.php"

SimpleXML - "Node no longer exists"

I'm trying to get the video data from this youtube playlist feed and add the interesting data to an array and use that later, but as you can see from the feed some videolinks are "dead" and that results in problems for my code.
The error I get is "Node no longer exists" when I try to access $attrs['url']. I've tried for hours to find a way to check if the node exists before I access it but I have no luck.
If anyone could help me to either parse the feed some other way with the same result or create a if-node-exists check that works I would be most happy. Thank you in advance
$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';
$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$videoobj[$i]['url'] = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = $attrs['url'];
$videoobj[$i]['title'] = $media->group->title;
$i++;
}
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = strval($attrs['url']);
$videoobj[$i]['title'] = strval($media->group->title);
}
SimpleXML's methods always return objects, which are themselves linked to the original document (some internal thingy related to libxml.) If you want to store that data for later use, cast it as a string, like this:
$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url'];
$videoobj[$i]['title'] = (string) $media->group->title;

Categories