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
Related
I am using Instagrams rest API and making this http request to get photos taken at certain lat and lng paramaters.
eg
https://api.instagram.com/v1/media/search?lat=51.5073509&lng=-0.1277583&access_token='access-token'
Is there a parameter I can add to this so only pictures from users which I (or the user identified from their access token) follow, or pictures which I have liked, are returned.
Thanks
You cannot just get posts from users you follow or just the ones you have liked.
You will get all public posts, you can check the user_has_liked=ture/false key in the json response for each posts to see if you have liked it.
To check if you follow the user or not, you will have to make another API for each of the post using the user_id:
https://api.instagram.com/v1/users/3/relationship?access_token=XXX
and look for outgoing_status in json response
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.
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
how can i use php to get youtube user subscription channel videos and display all the channel feeds, i want create a class or function where if i pass a user name the php class would retrieve the latest uploads / activities in the user subscribed channels and display the url plus the title and the image, also i want to be able to pass the title and url to other functions so that i can save it to database. on a another page i found this Pulling in YouTube Videos from Specific Channel but its for only one specific channel.
The relevant requests are for the newsubscriptionvideos feed in the YouTube GData API v2 or activities.list() in the newer YouTube Data API v3.
newsubscriptionvideos might not be natively support in the Zend GData client library (I don't see it mentioned in the developer's guide), so if you go that route, you should probably use alt=jsonc to get back an easy-to-parse JSON feed of the data. E.g, https://gdata.youtube.com/feeds/api/users/jeffposnicktest/newsubscriptionvideos?v=2&alt=jsonc
The alternative is to use v3's PHP client library to call activities.list().
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