【问题标题】:How to work with youtube api V3如何使用 youtube api V3
【发布时间】:2016-01-29 17:49:27
【问题描述】:

我需要有关 youtube API V3 的帮助。 就像我在浏览器上输入: https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=Titanic%201997%20Official%20Trailer&key= 它显示返回值。 但是我正在尝试从 php.ini 中收集数组。

如何在 php 中使用 GET https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=Titanic%201997%20Official%20Trailer&key= 以便在 php 中得到结果?

或者是否有任何选项可以获取 rss 提要格式的搜索结果。

提前致谢

【问题讨论】:

    标签: api youtube get


    【解决方案1】:

    在 YouTube API 上进行简单的视频搜索,您将获得视频标题、描述、视频 ID 和图像源,因此最佳方法完整代码如下所示。

    <?php
    
    error_reporting(0);
    
    $search = "Search Query"; // Search Query
    
    $api = "YouTube API Key"; // YouTube Developer API Key
    
    $results = 10; // Max results
    
    $link = "https://www.googleapis.com/youtube/v3/search?safeSearch=moderate&order=relevance&part=snippet&q=".urlencode($search). "&maxResults=$results&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>";
            }
    }
    ?>
    

    【讨论】:

    • 用于搜索视频(相关性)使用此 url,您可以获得 videoId 和标题、图像 url 等。googleapis.com/youtube/v3/… KEY&type=video&videoSyndicated=true&key=YOUR API KEY&maxResults=25
    • googleapis.com/youtube/v3/…{SERCH KEY}&type=video&videoSyndicated=true&key={YOUR API KEY}&maxResults=25
    • 如何格式化 url 以提交多个 parts 参数,因为这对我不起作用?
    猜你喜欢
    • 2015-07-29
    • 2016-11-13
    • 1970-01-01
    • 2012-12-09
    • 2016-09-07
    • 2017-07-25
    • 2016-04-14
    • 2016-09-05
    • 2021-07-28
    相关资源
    最近更新 更多