Skip to content

Instantly share code, notes, and snippets.

@jarridlima
Last active April 15, 2021 02:23
Show Gist options
  • Save jarridlima/a089f25e8a796e6b5f6f0f4d62cde395 to your computer and use it in GitHub Desktop.
Save jarridlima/a089f25e8a796e6b5f6f0f4d62cde395 to your computer and use it in GitHub Desktop.
Webhook Youtube Video
<?php
// https://pubsubhubbub.appspot.com/
// https://pubsubhubbub.github.io/PubSubHubbub/pubsubhubbub-core-0.4.html
// https://developers.google.com/youtube/v3/guides/push_notifications
// https://indieweb.org/How_to_publish_and_consume_WebSub#How_to_Subscribe
if(isset($_GET['hub_challenge'])){
// Resposta para verificação do webhook
echo $_GET['hub_challenge'];
// Registra em arquivo de logs
$file = fopen(__DIR__ . '/video_logs.txt','a+');
fwrite($file, "\r\n ============================== \r\n\r\n");
fwrite($file, json_encode($_GET));
fclose($file);
} else {
// Faz o tratamento das atualizações recebidas
$data = file_get_contents('php://input');
parseYouTubeUpdate($data);
}
function parseYoutubeUpdate($data) {
$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
// Organizando dados recebidos
$video_data = [
"xml" => $xml,
"id" => substr((string)$xml->entry->id, 9),
"title" => (string)$xml->entry->title,
"channel_id" => substr((string)$xml->entry->author->uri, 32),
"channel_url" => (string)$xml->entry->author->uri,
"author" => (string)$xml->entry->author->name,
"published" => (string)$xml->entry->published,
"updated" => (string)$xml->entry->updated
];
// Registra em arquivo de logs
$file = fopen(__DIR__ . '/video_logs.txt','a+');
fwrite($file, "\r\n ============================== \r\n\r\n");
fwrite($file, json_encode($video_data));
fclose($file);
return;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment