- you need to send a request to subscribe to the channel
- callback that modifies a subscription request and receives actual update data
subscribe to the function:
subscribe.php might look like this:
<?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; }
after sending the request, the pusub service will call youtube_subscribe_callback.php to confirm the subscription, it will use the GET method and it expects to receive a response that is "hub_challenge". after that, if you upload the video to your test channel, youtube_subscribe_callback.php will receive a POST request with the data.
so youtube_subscribe_callback.php (defined in the subscribeYoutubeChannel function) might look like this:
<?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 ); }
source share