dongmouhao7438 2017-12-28 13:52
浏览 179
已采纳

如何使用php和API从url获取youtube视频的缩略图

Below code, I got title and description but I want image as well as how to get the main image of video from JSON data.

$videoid = 'cMC_PtgKDJE';
$apikey = 'xyzxyzxyz';  //My API key

$json = file_get_contents('https://www.googleapis.com/youtube/v3/videos?id='.$videoid.'&key='.$apikey.'&part=snippet');
$ytdata = json_decode($json);
print_r($ytdata);
echo '<h1>Title: ' . $ytdata->items[0]->snippet->title . '</h1>';
echo 'Description: ' . $ytdata->items[0]->snippet->description;
  • 写回答

2条回答 默认 最新

  • doupiao1893 2017-12-28 13:55
    关注

    Each YouTube video has 4 generated images.You can create the url as fallows

    https://img.youtube.com/vi/<video-id-here>/0.jpg
    

    DEMO

    If you want high quality image then you can read following atricle

    how to get a youtube video’s thumbnail image in high quality

    How to get video id from youtube url

    $url = "http://www.youtube.com/watch?v=7zWKm-LZWm4&feature=relate";
    parse_str( parse_url( $url, PHP_URL_QUERY ), $url_vars );
    echo $url_vars['v'];    
      // Output: 7zWKm-LZWm4
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?