Search YouTube videos using YouTube API 3.0

In this post, I’ll share how to search YouTube videos by using YouTube Data API v3 and PHP. I’ll show how to search videos by type (video, channel, playlist), keywords and other search terms. In addition to this, I’ll show how to order the result by various parameters.

After reading this article, you can make search queries like getting list of videos from specific channel.  Also you can filter videos by keywords and other parameters.

In order to start, you need YouTube API Key for YouTube Data API Requests. If you don’t have API key, follow our previous articles Retrieve YouTube playlist videos using YouTube API and Get YouTube Video Info using YouTube API via PHP.

API Request URL

The request URL for Search API is as follows:

GET https://www.googleapis.com/youtube/v3/search

You need to pass parameters as query string along with API Key. There are several parameters Search API supports. Among them, key and part are required parameters.

The ‘key’ is the API KEY  you need to access YouTube DATA API.
The ‘part’ specifies a comma-separated list of one or more search resource properties that the API response will include.

Some of the notable parameters are as follows:

  • q
  • type
  • channelId
  • publishedAfter
  • publishedBefore
  • maxResults
  • order

So far, we have discussed on API Request URL and its parameters. Now, it’s time for action.

Search YouTube videos using keywords

In order to search using keywords, you need to specify the query term in q parameter. In addition to this, you can also restrict a search query to only retrieve a particular type of resource using type parameter.

There are 3 type of resource types: channel, playlist and video. You can also specify multiple resource types as a comma separated value.

The following  request shows search query using keyword ‘swimming’:

https://www.googleapis.com/youtube/v3/search?part=snippet&q=swimming&type=video&key=API_KEY

You can easily grab the response of above request using file_get_contents using PHP to use search result in your application.

Getting list of videos from channel using API

Similarly, you can get list of videos from a YouTube channel using API. For this, you need to specify channelId parameter of your YouTube channel.

Here’s the sample request:

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UC3VyA8KN_VgCF93EurnAQXw&key=API_KEY




Limiting and Ordering Search Result

So far, I have discussed how to compose search request to perform YouTube video search. However, it’s not sufficient in real life application. You will need limiting as well as ordering the search result.

To achieve this, we can add ‘maxResults’ and ‘order’ parameters to the search request.

The maxResults parameter specifies the maximum number of items that should be returned in the result set. It can fetch maximum 50 items. Default value is 5.

The order parameter specifies the method that will be used to order resources in the API response. Acceptable values are: date, rating, relevance, title, videoCount and viewCount. By default, relevance is considered.

Conclusion

In this way, we can perform YouTube video search and restrict our search result to certain criteria.

Refer YouTube Search API documentation to fine tune Search Request.

Leave a Reply