如何使用php在网站上嵌入youtube频道视频

时间:2015-07-25 17:08:41

标签: php iframe youtube embed channel

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"
   }
  }
 ]
}

请问有人可以解释一下 真的很谢谢你!! PS。对不起第二个巨大的代码报价

2 个答案:

答案 0 :(得分: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
            }
        }
    }
?>

答案 1 :(得分:0)

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变量,以在您网站的频道上生成视频列表。