0

I am using Use youtube API to show videos against a keyword in PHP. it's working fine while getting video title and id. But I want to show videos with a thumbnail along with the video title. Here is the code:

<?php
$api_key = 'AIzaSyAjowKxG2mRdr7p1N-3RtBd-BWAgjbf87g';
  
$arr_list = array();
if (array_key_exists('q', $_GET) && array_key_exists('max_result', $_GET) && array_key_exists('order', $_GET)) {
    $formatted_keyword = implode("+", explode(" ", $_GET['q']));
    $url = "https://www.googleapis.com/youtube/v3/search?q=$formatted_keyword&order=". $_GET['order'] ."&part=snippet&type=video&maxResults=". $_GET['max_result'] ."&key=". $api_key;
  
    if (array_key_exists('pageToken', $_GET)) {
        $url .= "&pageToken=". $_GET['pageToken'];
    }
 
    $arr_list = getYTList($url);
}
  
function getYTList($api_url = '') {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $arr_result = json_decode($response);
    if (isset($arr_result->items)) {
        return $arr_result;
    } elseif (isset($arr_result->error)) {
        //print error $arr_result->error
    }
}
?>
 
<form method="get">
    <p><input type="text" name="q" placeholder="Enter keyword" value="<?php if(array_key_exists('q', $_GET)) echo $_GET['q']; ?>" required></p>
    <p><input type="number" name="max_result" placeholder="Max Results" min="1" max="50" value="<?php if(array_key_exists('max_result', $_GET)) echo $_GET['max_result']; ?>" required></p>
    <p>
        <?php $arr_orders = ['date', 'rating', 'relevance', 'title', 'viewCount' , 'Video']; ?>
        <select name="order" required>
            <option value="">--SELECT ORDER--</option>
            <?php foreach ($arr_orders as $order) { ?>
                <option value="<?php echo $order; ?>" <?php if(array_key_exists('order', $_GET) && ($order==$_GET['order'])) echo 'selected'; ?>><?php echo ucfirst($order); ?></option>
            <?php } ?>
        </select>
    </p>
    <p><input type="submit" value="Submit"></p>
</form>
 
<?php
if (!empty($arr_list)) {
    echo '<ul>';
    foreach ($arr_list->items as $item) {
        echo "<li>". $item->snippet->title ." (Video ID: ". $item->id->videoId .")</li>";
    }
    echo '</ul>';
  
    $url = "?q=". $_GET['q'] ."&max_result=". $_GET['max_result'] ."&order=". $_GET['order'];
    if (isset($arr_list->prevPageToken)) {
        echo '<a href="'.$url.'&pageToken='.$arr_list->prevPageToken.'">Previous</a>';
    }
  
    if (isset($arr_list->nextPageToken)) {
        echo '<a href="'.$url.'&pageToken='.$arr_list->nextPageToken.'">Next</a>';
    }
}

?>

I am confused about where and how to add code about that or from where to modify this code, please help. Thanks.

  • 1
    Does this answer your question? [How do I get a YouTube video thumbnail from the YouTube API?](https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api) – Michael W. Nov 09 '21 at 18:52
  • Thanks for reply I will check but i don't think so it works. Basically i have a search box on the top when i type a keyword video with thumbnail and title have to come but i am getting only title and video id. – Faisal Qureshi Nov 10 '21 at 05:35
  • You just need the video id to grab the thumbnail. – Michael W. Nov 10 '21 at 15:41

0 Answers0