Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal, gostaria de saber se tem alguma forma de, com o link do video, sabe se um video está funcionando.
Preciso que o php verifique se o vídeo foi deletado, usuario saiu, conta foi banida etc. Quero saber se o video está pronto para ser visto.
Desde já agradeço.
Não tendi. e como eu vou saber se ele pode ser visto?
você vai colocar no final https://gdata.youtube.com/feeds/api/videos/H9uM9yAaAuY?v=2 (parte em negrito)
o id do video uqe você quer verificar, isso vai te retornar um xml e você pode le-lo com SimpleXML, dentre os campos que retornar um deles irá falar se est adisponivel ou n, basta testar
Não tendi. e como eu vou saber se ele pode ser visto?
Analise o XML e pesquise pela tag state, do namespace yt. Se essa tag existir então o vídeo não estará disponível. O atributo name dará um código sobre o estado e o conteúdo da tag uma descrição.
:seta: https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:state
Um exemplo simples de como verificar:
<?php
/**
* The <yt:state> tag contains information that describes the
* status of a video that cannot be played. Video entries that
* contain a <yt:state> tag are not playable. The name and reasonCode
* attributes and the tag value provide insight into the reason why
* the video is not playable.
*
* @param string $atom The XML API response
* @return boolean TRUE if the youtube video is playable
*/
function isYoutubeVideoPlayable($atom) $dom = new DOMDocument();
$dom->loadXML($atom);
$xpath = new DOMXPath($dom);
return $xpath->query('.//yt:state')->length == 0;
}
/**
* Gets the video xml using the Youtube API
*
* @param string $videoId The youtube video ID
* @param string $version The Youtuve API version
* @return string The Youtube video atom
*/
function getYoutubeVideoAtom($videoId, $version = '2') $endpoint = 'https://gdata.youtube.com/feeds/api/videos/' . $videoId;
$endpoint .= '?v=' . $version;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$atom = curl_exec($curl);
if (curl_errno($curl) != 0) {
//LOG: curl_error($curl)
}
curl_close($curl);
return $atom;
}
//O vídeo abaixo é reproduzível
//$videoId = 'H9uM9yAaAuY';
//O vídeo abaixo não é reproduzível
$videoId = 'NcbAibPA2yY';
//Pega o XML
$atom = getYoutubeVideoAtom($videoId);
//Verifica se é reproduzível
if (!isYoutubeVideoPlayable($atom)) {
echo 'The Youtube video "' . $videoId . '" is not playable.';
}
;)
Utilize a API do Youtube: https://developers.google.com/youtube/2.0/developers_guide_protocol_video_entries
https://gdata.youtube.com/feeds/api/videos/H9uM9yAaAuY?v=2
Isso vai te retornar um XML (Atom), basta parsear com DOM, XPath, SimpleXML ou qualquer coisa.