Youtube API - 订阅推送通知 [英] Youtube API - Subscribing to Push Notifications
问题描述
我的最终目标是在 YouTube 用户上传视频时设置一个 Webhook.经过一番研究,我阅读了这篇文章.
My final goal is to set up a Webhook whenever a YouTube user uploads a video. After some research I got to this article.
但是当我到达 https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID
部分时,我得到了 受限主题
尝试订阅 Google/SuperFeedr 集线器时出错.我的回调 URL 也正常工作.
But when I get to the part https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID
, I got Restricted topic
error when trying the subscribe to the Google/SuperFeedr hubs. I also got my callback URL working.
我想订阅的主题是:https://www.youtube.com/xml/feeds/videos.xml?channel_id=UC7T8roVtC_3afWKTOGtLlBA
通过浏览器访问时不显示任何内容.
Which shows nothing upon visiting via a browser.
我做错了吗?我已经挣扎了几个小时了,任何帮助表示赞赏.谢谢!
Am I doing something wrong? I've been struggling for a few hours now, any help is appreciated. Thanks!
更新:我发现了这个,但是这些提要没有 rel="hub"
属性,所以如果我想将其订阅到 hub 可能没用.
UPDATE: I found this, but those feeds doesn't have the rel="hub"
attribute, so probably useless if I want to subscribe it to hub.
推荐答案
- 您需要发送订阅频道的请求
- 改变订阅请求并获取实际更新数据的回调
订阅功能:
subscribe.php 可能如下所示:
<?php
function subscribeYoutubeChannel($channel_id = null, $subscribe = true) {
$subscribe_url = 'https://pubsubhubbub.appspot.com/subscribe';
$topic_url = 'https://www.youtube.com/xml/feeds/videos.xml?channel_id={CHANNEL_ID}';
$callback_url = 'http://' . $_SERVER['SERVER_NAME'] . str_replace(basename($_SERVER['REQUEST_URI']), '', $_SERVER['REQUEST_URI']) . 'youtube_subscribe_callback.php';
$data = array(
'hub.mode' => $subscribe ? 'subscribe' : 'unsubscribe',
'hub.callback' => $callback_url,
'hub.lease_seconds'=>60*60*24*365,
'hub.topic'=> str_replace(array(
'{CHANNEL_ID}'
), array(
$channel_id
), $topic_url)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($data)
)
);
$context = stream_context_create($opts);
@file_get_contents($subscribe_url, false, $context);
return preg_match('200', $http_response_header[0]) === 1;
}
请求发送后,pusub服务会调用youtube_subscribe_callback.php来验证订阅它将使用 GET 方法,并希望收到hub_challenge"的答案.之后,如果您将视频上传到您的测试频道 youtube_subscribe_callback.php 将收到带有数据的 POST 请求.
after the request is sent, the pusub service will call youtube_subscribe_callback.php to verify the subscription it will use the GET method and it expects to receive an answer which is "hub_challenge". after that if you upload a video to your test channel youtube_subscribe_callback.php will receive POST request with data.
所以 youtube_subscribe_callback.php(在 subscribeYoutubeChannel 函数中定义)可能如下所示:
so youtube_subscribe_callback.php (defined in subscribeYoutubeChannel function) may look like:
<?php
if (isset($_GET['hub_challenge'])) {
echo $_REQUEST['hub_challenge'];
} else {
$video = parseYoutubeUpdate(file_get_contents('php://input'));
}
function parseYoutubeUpdate($data) {
$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
$video_id = substr((string)$xml->entry->id, 9);
$channel_id = substr((string)$xml->entry->author->uri, 32);
$published = (string)$xml->entry->published;
return array(
'video_id'=>$video_id,
'channel_id'=>$channel_id,
'published'=>$published
);
}
这篇关于Youtube API - 订阅推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!