How do I get YouTube channel identifier from a channel link? - PHP - php

I'm trying to get a YouTube channel's username or ID from their channel link. Some channels have unique usernames and some channels have random IDs. Is there a method to retrieve this ID in a string format from a given link to their channel using PHP?

The Channel resource (https://developers.google.com/youtube/v3/docs/channels) has a id property that YouTube uses to uniquely identify the channel.
You can use the Channel's list method (https://developers.google.com/youtube/v3/docs/channels/list) which will search for Channels based on a search criteria you provide. One possible search criteria is a username parameter. Once you get the channel that corresponds to that username, you can get the id property from that channel.

Related

Embedding YouTube Channel Details using PHP

I want to create a webpage on my site that acts as a gallery for users YouTube channels, so it will display their channel profile picture, channel title and possibly a few other bits of info.
The way thought to achieve this was to get the user to insert their YouTube Channel URL when they sign up (if they have one) which I have a function that will separate the channel name from the URL and store the URL and the channel name in my MySQL database.
With this info can I use the YouTube API to request the profile picture and other data using PHP? I have setup my YouTube API and have my API key but there is so much junk and info on the YouTube pages that I can't figure it out and I'm not familiar with JSON.
Question: How can I request data from YouTube using the API and the Channel URL/Channel Name?
You can use Channels:list method to search for a collection of channel resources that will match your request criteria
You can use forUsername or id request parameter as one of your filters for channels:list method.
forUsername
string
The forUsername parameter specifies a YouTube username, thereby requesting the channel associated with that username.
id
string
The id parameter specifies a comma-separated list of the YouTube channel ID(s) for the resource(s) that are being retrieved. In a channel resource, the id property specifies the channel's YouTube channel ID.
Note:
Specify exactly only one of the filters available for the channels:list request
Channel:list method will return a response body with the following structure:
{
"kind": "youtube#channelListResponse",
"etag": etag,
"nextPageToken": string,
"prevPageToken": string,
"pageInfo": {
"totalResults": integer,
"resultsPerPage": integer
},
"items": [
channel Resource
]
}
items[]
A list of channels that match the request criteria.
For a list of information/data that you could access in the channels resource object,
Please refer here:
https://developers.google.com/youtube/v3/docs/channels
Quickstart reference on how to use Youtube API in PHP:
https://developers.google.com/youtube/v3/quickstart/php
Channels:List method information could be found here:
This contains request and response parameters.
https://developers.google.com/youtube/v3/docs/channels/list

Search Facebook Friends by name using Graph API

I know how to search name in our MySQL Database using %search%. But using Graph API, how can we search user's facebook friends by name? I am using taggable_friends to get list of all user's friends. Is there a way to call Facebook Graph's API using %search% and get the result JSON with matching friend's names?
You can use the Search API to search for users by name:
https://developers.facebook.com/docs/graph-api/using-graph-api#search

Get most popular videos of a channel by its name

I'm trying to list most popular (or newest) videos of a channel by its name using YouTube 3.0 API.
I already implemented this, just not sure if it is correct way to do this, so need an advice.
First of I find channel ID by name (e.g. TEDxTalks in place of {channel_name}):
https://www.googleapis.com/youtube/v3/search?part=snippet&type=channel&q={channel_name}&key={API_KEY}
Then I get videos of this channel ID, ordered by viewCount (or date), which perfectly lists videos:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channel_id}&order=viewCount&type=video&key={API_KEY}
How reliable is the first request? If name is the exact match of channel url slug or username (e.g. tedxtalks), will it always return corresponding channel? Or is there any more reliable way to get channel ID by its name?
Thank you.
Search API is not for exact match query. And it searching for all channel title(username found in URL if exist), channel name(xxx added 1 video) and even channel ID!
To answer your question, unfortunately it's not reliable and no other more reliable way.
A quick example, try to query username "abc".
https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=abc&type=channel&key={YOUR_API_KEY}
The Search API response will return ABCNetwork, ABCNews, udruzenjeabc... and so on. No channelTitle(username) "abc" in the first 50 items.
Even though not all channels have username. If you got username of specific channel, then you should use Channel API for exact match query,
https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername=abc&maxResults=5&key={YOUR_API_KEY}
You might wonder why YouTube API doesn't provide exact search for "channel name"(xxx added 1 video)? It's because channel name is not unique, as a person name is not unique.
As indicated at https://developers.google.com/youtube/v3/guides/working_with_channel_ids:
If you are using v3 and want to retrieve the channel ID that
corresponds to the currently authorized user, you can call the
channels.list(part="id", mine=true) method. This is equivalent to
asking for the channel profile of the default user in v2.
If you ever do find yourself with an arbitrary legacy YouTube username
that you need to translate into a channel ID using v3 of the API, you
can make a channels.list(part="id", forUsername="username") call to
the API.
If you only know a display name and are looking to find the
corresponding channel, the search.list(part="snippet", type="channel",
q="display name") method will come in handy. You should be prepared to
deal with the possibility of the call returning more than one item in
the response, since display names are not unique.
Finally, i have no idea why YouTube didn't force to generate a unique readable username (like how Facebook add a random number at the end of duplicated username, e.g. zuck.5 and zuck.21 ) for all new user.
References:
https://developers.google.com/youtube/v3/docs/search/list
https://developers.google.com/youtube/v3/docs/channels/list#try-it
https://support.google.com/youtube/answer/2657968?hl=en
https://developers.google.com/youtube/v3/guides/working_with_channel_ids

API Youtube, how to get like and dislikes

On a website, i use API Youtube with ZendGdata for retrieve all video of a user and create webpage with the video and his informations.
Now i need to add a system for like or dislikes video on my website, and i need to retrieve the number of likes and dislikes.
(Yes, there are 2 questions here^^)
So, i retrieve for example title with $videoEntry->getVideoTitle();, number of views with $videoEntry->getVideoViewCount(); but how i can retrieve number of likes/dislikes ?
And how i can vote for a video on my website ?
If you take a look at the videos resource in YouTube 3.0 API, among the values included are:
likeCount
dislikeCount
Full details here: https://developers.google.com/youtube/v3/docs/videos
If you're using the YouTube 2.0 API, then check out the <yt:rating> tag, which contains the following fields:
numLikes
numDislikes
More info here: https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:rating
An update to Cormac answer is that youtube will remove this and you can use it for yourself only.
Note: The statistics.dislikeCount property will be made private as of December 13, 2021. This means that the property will only be included in an API response if the API request was authenticated by the video owner. See the revision history for more information.

Getting demographics info about youtube channels using youtube api

I need to access Youtube channel's demographics data with oauth.
One problem is that Youtube api called insight only provides zipped folders with csv files.
While I can probably download the zip, unzip, access the file, and delete the original once I get the data I need, I was wondering if there is any other way to get youtube channel's demographics data.
I believe that yes, using the new YouTube Analytics API:
https://developers.google.com/youtube/analytics/v1/available_reports
From the doc:
Channel reports
The table below lists the different types of channel reports accessible via the API. To retrieve a channel report, set the ids parameter value to channel==USER_ID, where USER_ID specifies the YouTube user ID of the currently authenticated user.
And I think that is the report you wants:
dimensions: country
metrics: views, comments, favoritesAdded, favoritesRemoved, likes, dislikes, shares, subscribersGained, subscribersLost
filters: video
or this one:
dimensions: ageGroup, gender
metrics: viewerPercentage
filters: country video(,country)
and this example shows how to call the API:
https://developers.google.com/youtube/analytics/v1/sample-application

Categories