YouTube APIs are the most popular APIs and daily hundreds of users from around the world search for it on the internet. In this article I am describing the simplest procedure to use YouTube Data API.
Use the YouTube API with PHP
First thing you need is to create an account on Google or if you have Gmail account, that is enough. To use any Google API, one first needs to go to Google Developer Console and create a project there.
Please click on the link: https://console.developers.google.com/
On dashboard page, you will see a list of all APIs provided by Google, including the 3 APIs for using YouTube functionality.
To use YouTube Data API, click YouTube Data API link.
On next page click Create project to create a project first.
Now after the creation of project, click Enable button to enable the API for usage.
Next step is to get credentials to use the API, click Go to credentials button. Select suitable values for you (I chose Web browser (JavaScript), public data) and click What credentials do I need?.
Select Browse key 1, click Create API key to get your API key. Copy and save it for later use inside the code.
Here is an example code to get the list of popular videos on YouTube.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $api_key = '****insert your API KEY here******'; $ch = curl_init("https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular&key=".$api_key); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0 ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); $result = curl_exec($ch); $response = json_decode($result); echo "<pre>"; print_r($response); ?> |
Related Topics:
How to Embed YouTube Video Into Your Website
How to Embed a Youtube Video With Muted Sound on Website
For Complete API documentation, Please Follow:
https://developers.google.com/youtube/v3/docs/videos/list
Just read the API documentation and with some changes in the above code, you can easily get much more information about YouTube videos.