【问题标题】:how to embed youtube channel video on website with php如何使用 php 在网站上嵌入 youtube 频道视频
【发布时间】:2015-10-16 05:14:57
【问题描述】:

在 youtube api v3 更新后,我不知道如何修复我网站的视频部分。 该部分显示在我的 youtube 频道 UCBL3ecWukz_EX7FrDmtdgJQ 上传的视频

我已经创建了 OAuth:Web 应用程序的客户端 ID,公共 API 访问:服务器应用程序的密钥和浏览器应用程序的密钥。

现在我正在尝试了解如何使用它们来检索上传到我的频道的视频列表。

我试过这段代码

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCBL3ecWukz_EX7FrDmtdgJQ&maxResults=20&key=***************************************

现在我不知道如何处理输出.. 类似的东西

{
 "kind": "youtube#searchListResponse",
 "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/6M9Lo8BzT3ingng1jJF2bBh1CNs\"",
 "pageInfo": {
  "totalResults": 5,
  "resultsPerPage": 20
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/LHUK9eLdq9H2-Szizgc5BvV-gxM\"",
   "id": {
    "kind": "youtube#channel",
    "channelId": "UCBL3ecWukz_EX7FrDmtdgJQ"
   },
   "snippet": {
    "publishedAt": "2014-12-26T11:20:46.000Z",
    "channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
    "title": "Parrocchia Sant'Agata Alì",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "https://lh5.googleusercontent.com/-O78yDecTsmk/AAAAAAAAAAI/AAAAAAAAAAA/U-NQ4NkzZQE/photo.jpg"
     },
     "medium": {
      "url": "https://lh5.googleusercontent.com/-O78yDecTsmk/AAAAAAAAAAI/AAAAAAAAAAA/U-NQ4NkzZQE/photo.jpg"
     },
     "high": {
      "url": "https://lh5.googleusercontent.com/-O78yDecTsmk/AAAAAAAAAAI/AAAAAAAAAAA/U-NQ4NkzZQE/photo.jpg"
     }
    },
    "channelTitle": "",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/YPX-4TZwL2BMwOoIP3pO2_KBzvs\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "FjGA6cjh5zI"
   },
   "snippet": {
    "publishedAt": "2015-02-12T23:45:39.000Z",
    "channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
    "title": "Alì S  Agata Bacio del braccio",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/FjGA6cjh5zI/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/FjGA6cjh5zI/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/FjGA6cjh5zI/hqdefault.jpg"
     }
    },
    "channelTitle": "",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/-m38lEymXlK8ZVMm2dNRzX8e8ro\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "7PQBjOedbtk"
   },
   "snippet": {
    "publishedAt": "2015-02-07T20:43:40.000Z",
    "channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
    "title": "Annuncio Festa Grande",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/7PQBjOedbtk/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/7PQBjOedbtk/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/7PQBjOedbtk/hqdefault.jpg"
     }
    },
    "channelTitle": "",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/Bv1fyl1c4aVHIrWuOV5gNf7xpMI\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "Gmelq3hGEPI"
   },
   "snippet": {
    "publishedAt": "2015-02-15T23:17:22.000Z",
    "channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
    "title": "Alì S  Agata riposta sull'altare",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/Gmelq3hGEPI/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/Gmelq3hGEPI/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/Gmelq3hGEPI/hqdefault.jpg"
     }
    },
    "channelTitle": "",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/LZFjbm4TuWdxvls0Jzan6H6Ow5A\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "GMizgWpyt2k"
   },
   "snippet": {
    "publishedAt": "2015-03-27T22:13:32.000Z",
    "channelId": "UCBL3ecWukz_EX7FrDmtdgJQ",
    "title": "Alì Chiesa S  Agata Preghiera per devozione Padre Serafino",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/GMizgWpyt2k/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/GMizgWpyt2k/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/GMizgWpyt2k/hqdefault.jpg"
     }
    },
    "channelTitle": "",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

谁能给我解释一下 真的很感谢你!! 附言。对不起第二个巨大的代码引用

【问题讨论】:

    标签: php iframe youtube embed channel


    【解决方案1】:

    真是巧合……我几天前才需要这个 :) 这是我使用的代码:

    <?php
        $playlistID = ''; // The Playlist ID for your 'uploads'-playlist.
        $videofile = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=10&playlistId=' . $playlistID;
        $json = file_get_contents($videofile);
        $json = preg_replace('/^\xEF\xBB\xBF/', '', $json);
        $array = json_decode($json, TRUE);
    
        foreach ($array as $key => $value) {
            if ($key == "items") {
                echo "<table class='vtable'>";
                for ($i = 0; $i < count($array["items"]); $i = $i + 1) {
                    $id = $array["items"][$i]['snippet']['resourceId']['videoId'];
                    $thumbnail = $array["items"][$i]['snippet']['thumbnails']['high']['url'];
                    $title = $array["items"][$i]['snippet']['title'];
                    $description = $array["items"][$i]['snippet']['description'];
                    // do some stuff with the variables
                }
            }
        }
    ?>
    

    【讨论】:

    • 感谢您的回复!! for 行给出错误:PHP 致命错误:不能使用 stdClass 类型的对象作为数组
    • 这很奇怪......似乎你得到的是一个对象而不是一个数组......尝试用$array-&gt;x替换所有$array["x"]部分
    【解决方案2】:

    1) 从 PHP 发出 HTTP 请求以获取 JSON 数据作为字符串。使用 cURL、像 Guzzle 这样的库或像 file_get_contents() 这样的内置函数来执行此操作。参考:http://php.net/manual/en/book.curl.php

    $youtube_api_string = file_get_contents("http://your_api_url");
    

    2) 使用

    将该字符串转换为标准对象
    $api_result = json_decode($youtube_api_string); 
    

    参考:http://php.net/manual/en/function.json-decode.php

    3) 编写一个循环将您的搜索结果组合成可用的 YouTube 网址

    $youtube_urls = []; 
    
    for ($i = 0; $i < count($api_result->items); $i++) {
        $video = $api_result->items[$i];
        array_push($youtube_urls, ["url" => "http://youtube.com/watch?v=" . $video->id->videoId, "title" => $video->snippet->title ]);
    }
    

    从这里开始,您应该能够循环访问 $youtube_urls 变量以在您的网站上生成您频道上的视频列表。

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 2011-10-12
      • 2011-12-04
      • 2016-03-20
      • 2012-01-02
      • 2018-04-22
      • 2019-08-14
      • 2017-06-27
      • 2021-03-25
      相关资源
      最近更新 更多