我的最终目标是每当 YouTube 用户上传视频时就设置一个 Webhook。经过一番研究后,我看到了这篇文章。
但是当我到达
https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID
部分时,在尝试订阅Google/SuperFeedr中心时出现了Restricted topic
错误。我的回调 URL 也正常工作了。
我要订阅的主题是这样的:
https://www.youtube.com/xml/feeds/videos.xml?channel_id=UC7T8roVtC_3afWKTOGtLlBA
通过浏览器访问时不显示任何内容。
我做错了什么吗?我已经挣扎了几个小时了,非常感谢任何帮助。谢谢!
更新:我找到了this,但这些提要没有
rel=”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 请求。
so youtube_subscribe_callback.php(在 subscribeYoutubeChannel 函数中定义)可能如下所示:
<?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
);
}
这个过程通常是两个步骤,首先你进入subscribe页面,输入你的回调服务器url,主题url(基本上是你想收听的ytb频道的feed url,其他字段是可选的), pub 服务器将通过向您的回调服务器发出
GET
请求来验证您的订阅,在 go 中它可能如下所示:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
challenge := r.URL.Query().Get("hub.challenge")
if challenge != "" {
fmt.Fprintf(w, challenge)
}
})
然后,在每个新视频(或更新旧视频的标题、描述)上,酒吧将向您的服务器提交
POST
请求,正文中的 xml
看起来与此类似:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns="http://www.w3.org/2005/Atom">
<link rel="hub" href="https://pubsubhubbub.appspot.com" />
<link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCtEorrVfo4GQsN82HsrnKyk" />
<title>YouTube video feed</title>
<updated>2018-12-12T06:02:55.950497732+00:00</updated>
<entry>
<id>yt:video:_em_FFNUcvs</id>
<yt:videoId>_em_FFNUcvs</yt:videoId>
<yt:channelId>UCtEorrVfo4GQsN82HsrnKyk</yt:channelId>
<title>December 12, 20</title>
<link rel="alternate" href="https://www.youtube.com/watch?v=_em_FFNUcvs" />
<author>
<name>Ak Ram</name>
<uri>https://www.youtube.com/channel/UCtEorrVfo4GQsN82HsrnKyk</uri>
</author>
<published>2018-12-12T05:57:07+00:00</published>
<updated>2018-12-12T06:02:55.950497732+00:00</updated>
</entry>
</feed>
我认为指南的网址是错误的
https://www.youtube.com/xml/feeds/videos.xml
不再工作了。但如果没有 /xml
那么就可以工作了。
有样品。
https://www.youtube.com/feeds/videos.xml?channel_id=UCJHiO2Dfao-qpYiA79swtAA https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCJHiO2Dfao-qpYiA79swtAA