问问题
14509 次
3 回答
7
- 您需要发送订阅频道的请求
- 一个回调,它改变订阅请求并获取更新的实际数据
订阅功能:
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 请求。
所以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
);
}
于 2016-07-14T07:52:05.527 回答
3
该过程通常是两个步骤,首先进入订阅页面,输入您的回调服务器 url,主题 url(基本上是您要收听的 ytb 频道的提要 url,其他字段是可选的),发布服务器将通过对您的回调服务器的请求来验证您的订阅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>
于 2018-12-12T06:10:22.837 回答