Jump to content

YouTube Videocount help

Hades

I'm trying to pull the count of how many videos is uploaded onto an YouTube channel. I'm having problems, i want to show The number of videos uploaded to the channel. like this does with other statistics

 

<?php 
$data = json_decode($data, true);
$stats_data = $data['entry']['yt$statistics'];
 
echo 'lastWebAccess = '.$stats_data['lastWebAccess'].'<br />';
echo 'subscriberCount = '.$stats_data['subscriberCount'].'<br />';
echo 'videoWatchCount = '.$stats_data['videoWatchCount'].'<br />';
echo 'viewCount = '.$stats_data['viewCount'].'<br />';
echo 'totalUploadViews = '.$stats_data['totalUploadViews'].'<br />';
?>

 

ive been trying and looking for help all over the internet help me plz

 

update this is what i have come up with but it seams it doesn'twork 

 

<?php 
$json = json_decode($jsonString); 
print($json->data->totalItems . "\n"); 
?>
Link to comment
Share on other sites

Link to post
Share on other sites

You are decoding the wrong part of the JSON object I think.

Looking at the raw JSON code, you probably need this part:

{"rel":"http://gdata.youtube.com/schemas/2007#user.uploads","href":"http://gdata.youtube.com/feeds/api/users/imsparky15/uploads","countHint":242},
Using this code:

 

<?php$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/ImSparky15?alt=json');$data = json_decode($data, true);$stats_data = $data['entry']['gd$feedLink'];echo 'uploadcount = '.$stats_data[4]['countHint'].'<br />';?>
gets me:

uploadcount = 242<br />
which, judging from your YT channel, seems to be the number of uploaded videos

on your channel.

EDIT: Just saw your edit, I'll have a look at that as well.

EDIT 2:

Yeah, something doesn't look quite right in your second version. I recommend using

this code to get a closer look at the array which you get after decoding the json

object. This will allow you to really analyse the whole thing and take it apart into

those pieces which are relevant to you.

<?php$jsonString = file_get_contents('http://gdata.youtube.com/feeds/api/users/ImSparky15?alt=json');$json = json_decode($jsonString);echo print_r($json,true);?>

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

You are decoding the wrong part of the JSON object I think.

Looking at the raw JSON code, you probably need this part:

{"rel":"http://gdata.youtube.com/schemas/2007#user.uploads","href":"http://gdata.youtube.com/feeds/api/users/imsparky15/uploads","countHint":242},
Using this code:

 

<?php$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/ImSparky15?alt=json');$data = json_decode($data, true);$stats_data = $data['entry']['gd$feedLink'];echo 'uploadcount = '.$stats_data[4]['countHint'].'<br />';?>
gets me:

uploadcount = 242<br />
which, judging from your YT channel, seems to be the number of uploaded videos

on your channel.

EDIT: Just saw your edit, I'll have a look at that as well.

 

thank you so much this has been bugging me all day

Link to comment
Share on other sites

Link to post
Share on other sites

Happy to help. :)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

I'll have a look.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

I'll allow myself a double post so that you'll get the update notification ;)

I've taken a look at it, but seeing as how I've never worked with the YT API

before I think it would just take too long for me to work through all of that. I

know PHP and a bit of JSON, but the Youtube API is new to me, and I just don't have

the time to read all that documentation at the moment to really get his solution

to work (sorry).

Basically the idea of his suggestion is that you don't need to get the JSON object

but can instead make a direct request for the information to the server.

I highly recommend reading the linked documentation thoroughly, I'm pretty sure that

what you need to know is in there, I just don't have the time to work through all of

that at the moment. Or ask the guy on SO, he should be able to help as well.

What I have found out is that you will probably need your channel ID and your

account id, which you should be able to get at this page on YT.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

I'll allow myself a double post so that you'll get the update notification ;)

I've taken a look at it, but seeing as how I've never worked with the YT API

before I think it would just take too long for me to work through all of that. I

know PHP and a bit of JSON, but the Youtube API is new to me, and I just don't have

the time to read all that documentation at the moment to really get his solution

to work (sorry).

Basically the idea of his suggestion is that you don't need to get the JSON object

but can instead make a direct request for the information to the server.

I highly recommend reading the linked documentation thoroughly, I'm pretty sure that

what you need to know is in there, I just don't have the time to work through all of

that at the moment. Or ask the guy on SO, he should be able to help as well.

What I have found out is that you will probably need your channel ID and your

account id, which you should be able to get at this page on YT.

Ty I'm also new to YT api it was just confusing me thanks for all your help and the links

Link to comment
Share on other sites

Link to post
Share on other sites

I done what the other user said on the other site and got the same results, the only difference I see is the use of channel Id, but I'm confused on how to pull the video count in this document

https://www.googleapis.com/youtube/v3/channels?part=statistics&id={CHANNEL_ID}&fields=items%2Fstatistics&key={YOUR_API_KEY}

Response

{
"items": [
{
"statistics": {
"viewCount": "433592",
"commentCount": "30",
"subscriberCount": "2270",
"hiddenSubscriberCount": false,
"videoCount": "242"
}
}
]
}

Link to comment
Share on other sites

Link to post
Share on other sites

Good to know it's working. :)

Indeed extracting individual variables from this seems a bit more laborious than directly

parsing a JSON object (unless you could get it to directly and only return the number "242",

which would make things quite neat).

If that's not possible and you still wanted to process this in PHP you could store it in

a document and then parse that with regular expressions. But TBH that seems a bit inelegant

IMO.

So as far as I can tell at the moment the first version is still a bit more elegant for

processing data in PHP. But as said, I have no experience with YT's API, so there might

be a better way still.

EDIT: Discard, of course you can parse this as JSON. That's the whole point.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Good to know it's working. :)

Indeed extracting individual variables from this seems a bit more laborious than directly

parsing a JSON object (unless you could get it to directly and only return the number "242",

which would make things quite neat).

If that's not possible and you still wanted to process this in PHP you could store it in

a document and then parse that with regular expressions. But TBH that seems a bit inelegant

IMO.

So as far as I can tell at the moment the first version is still a bit more elegant for

processing data in PHP. But as said, I have no experience with YT's API, so there might

be a better way still.

Ty I'm having problems with the second version, the only benefit I see with it is using user ID, but the 1st version was easier to code in php

Link to comment
Share on other sites

Link to post
Share on other sites

Wrote this a while back in python... worked back then haven't tested it recently though 

 

 

 

def getUserChannelData(request, userID):

    try:
        path = "https://gdata.youtube.com/feeds/api/users/"+userID
        xmlDoc = urllib2.urlopen(path)
 
        tree = etree.parse(xmlDoc)
        root = tree.getroot() 
 
        tmpDataHolder = tree.findall("{http://schemas.google.com/g/2005}feedLink")
 
        upload_count=0
        for lnk in tmpDataHolder:
             if 'uploads' in lnk.attrib['href']:
                 upload_count = lnk.attrib['countHint']
 
    except:
        er='Invalid User Name Provided'
Link to comment
Share on other sites

Link to post
Share on other sites

im so surprised at the amount of attention this topic has got thank you everyone that is helping me

Link to comment
Share on other sites

Link to post
Share on other sites

Posting a solution from PM for the public. This works for me:

<?php$data = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCRO8jVq4et04MN-k8vOhlFg&fields=items%2Fstatistics&key=AIzaSyChIuEZJgrU9E1AViVCHyqqqBkb8dJ6yKA');$data = json_decode($data, true);$stats_data = $data['items']['0']['statistics'];echo 'uploadcount = '.$stats_data['videoCount'];?>

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Posting a solution from PM for the public. This works for me:

<?php$data = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCRO8jVq4et04MN-k8vOhlFg&fields=items%2Fstatistics&key=AIzaSyChIuEZJgrU9E1AViVCHyqqqBkb8dJ6yKA');$data = json_decode($data, true);$stats_data = $data['items']['0']['statistics'];echo 'uploadcount = '.$stats_data['videoCount'];?>

I'm getting this error with that code http://img202.imageshack.us/img202/9858/9v4s.jpg

Link to comment
Share on other sites

Link to post
Share on other sites

Ah yes, I stumbled upon that one myself. You need to uncomment the following

line in your php.ini:

For a Linux system (such as mine):

extension=openssl.so
And for a Windows environment (haven't tried this one personally, don't have

a Windows machine):

extension=php_openssl.dll
See this post on SO about it.

Depending on your server I am not sure if you actually have access to php.ini,

in which case this might get tricky. If that's the case, I'm not quite sure

how to get around that at the moment.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Ah yes, I stumbled upon that one myself. You need to uncomment the following

line in your php.ini:

For a Linux system (such as mine):

extension=openssl.so
And for a Windows environment (haven't tried this one personally, don't have

a Windows machine):

extension=php_openssl.dll
See this post on SO about it.

Depending on your server I am not sure if you actually have access to php.ini,

in which case this might get tricky. If that's the case, I'm not quite sure

how to get around that at the moment.

Thank you for all the help I will try this in the morning as it 3am in the UK and I'm sure it's much later were u are

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you for all the help I will try this in the morning as it 3am in the UK and I'm sure it's much later were u are

4 AM. I'm actually already in bed, but curiosity got the better of me and

I just had to check my feed one last time. :D

Anyway, good night!

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

4 AM. I'm actually already in bed, but curiosity got the better of me and

I just had to check my feed one last time. :D

Anyway, good night!

happy to tel u that its working now that i have uncomment openssl in my server php.ini

thanks for all the help

Link to comment
Share on other sites

Link to post
Share on other sites

is there a way to add (number_format), so the number is easier to read on output?

 

DW sorted it out

 

<?php
$data = json_decode($data, true);
$stats_data = $data['items']['0']['statistics'];
echo number_format($stats_data['viewCount']);
?>
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×