user3212459
user3212459

Reputation: 11

Youtube API 3 PHP Script Help needed

Could you please help me in a Youtube API 3 PHP Script.

I used this URL in my PHP script to fetch details of a particular video from youtube:

https://www.googleapis.com/youtube/v3/videos?id=$VideoID&part=snippet%2Cstatistics&key=$YoutubeApiKey

This works fine. My Question is Which URL to use to fetch Title, Description and Thumbnail image of the first 25 videos for a particular keyword.

I read youtube api documentation, but I could not figure out. Highly appreciate your help.

Upvotes: 1

Views: 667

Answers (3)

Muhammad Salman Zaib
Muhammad Salman Zaib

Reputation: 568

Make a simple video search on YouTube API and you will get video title, description, video id and image source so best approach full code is given below.

<?php
error_reporting(0);

$search = "Search Query"; // Search Query

$api = "YouTube API Key"; // YouTube Developer API Key

$link = "https://www.googleapis.com/youtube/v3/search?safeSearch=moderate&order=relevance&part=snippet&q=".urlencode($search). "&maxResults=25&key=". $api;

$video = file_get_contents($link);

$video = json_decode($video, true);

foreach ($video['items'] as $data){
        $title = $data['snippet']['title'];
        $description = $data['snippet']['title'];
        $vid = $data['id']['videoId'];
        $image = "https://i.ytimg.com/vi/$vid/default.jpg";

        // Output Title/Description/Image URL If Video ID exist
        if($vid){
            echo "Title: $title<br />Description: $description<br />Video ID: $vid<br />Image URL: $image<hr>";
        }
}
?>

Upvotes: 1

Syed Alish Raza Naqvi
Syed Alish Raza Naqvi

Reputation: 83

You need to change the URL format like this with limit of videos and detail

https://www.googleapis.com/youtube/v3/search?key={YOUR_API_KEY}&part=snippet&order=relevance&maxResults=25

Change the Max Result

Upvotes: 0

NIlay Mehta
NIlay Mehta

Reputation: 476

You can use the Youtube search API to do the same.

GET https://www.googleapis.com/youtube/v3/search?part=snippet&q=boating&key={YOUR_API_KEY}

Here is the more information on the same.

https://developers.google.com/youtube/v3/docs/search/list

Upvotes: 1

Related Questions