[RESOLU] Récupérer les infos vidéo Youtube

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : [RESOLU] Récupérer les infos vidéo Youtube

Re: Récupérer les infos vidéo Youtube

par cris84 » 13 janv. 2015, 19:44

Voici un code qui marche, ça peut aider...
<?php
//The Youtube's API url
define('YT_API_URL', 'http://gdata.youtube.com/feeds/api/videos?q=');
 
//Change below the video id.
$video_id = $_POST['url'];
 
//Using cURL php extension to make the request to youtube API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, YT_API_URL . $video_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//$feed holds a rss feed xml returned by youtube API
$feed = curl_exec($ch);
curl_close($ch);
 
//Using SimpleXML to parse youtube's feed
$xml = simplexml_load_string($feed);
 
$entry = $xml->entry[0];
//If no entry whas found, then youtube didn't find any video with specified id
if(!$entry) exit('Error: no video with id "' . $video_id . '" whas found. Please specify the id of a existing video.');
$media = $entry->children('media', true);
$group = $media->group;
 
$title = $group->title;//$title: The video title
$description = $group->description;
$viewcount = $group->viewcount;//$desc: The video description
$vid_keywords = $group->keywords;//$vid_keywords: The video keywords
$thumb = $group->thumbnail[0];//There are 4 thumbnails, the first one (index 0) is the largest.
//$thumb_url: the url of the thumbnail. $thumb_width: thumbnail width in pixels.
//$thumb_height: thumbnail height in pixels. $thumb_time: thumbnail time in the video
list($thumb, $thumb_width, $thumb_height, $thumb_time) = $thumb->attributes();
$content_attributes = $group->content->attributes();
//$vid_duration: the duration of the video in seconds. Ex.: 192.
$vid_duration = $content_attributes['duration'];
//$duration_formatted: the duration of the video formatted in "mm:ss". Ex.:01:54
$duration = str_pad(floor($vid_duration/60), 2, '0', STR_PAD_LEFT) . ':' . str_pad($vid_duration%60, 2, '0', STR_PAD_LEFT);

//echoing the variables for testing purposes:
echo 'Auteur : ' . $viewcount . '<br />';
echo 'Description : ' . $description . '<br />';
echo 'video keywords: ' . $vid_keywords . '<br />';
/echo 'thumbnail width: ' . $thumb_width . '<br />';
echo 'thumbnail height: ' . $thumb_height . '<br />';
echo 'thumbnail time: ' . $thumb_time . '<br />';
echo 'video duration: ' . $vid_duration . '<br />';
echo 'Durée : ' . $duration_formatted;

?>

Re: Récupérer les infos vidéo Youtube

par cris84 » 13 janv. 2015, 17:16

Mon code marchait jusqu'à hier...
L'as tu testé depuis car chez moi il fonctionne pas :?:

Re: Récupérer les infos vidéo Youtube

par tof73 » 13 janv. 2015, 16:58

une manière de faire qui fonctionne pour la description :
$content = @file_get_contents("http://gdata.youtube.com/feeds/api/videos?q=5Qm4sM8BPCs");
$xml = simplexml_load_string($content);
//print_r($xml);
echo $xml->entry->content; 

Récupérer les infos vidéo Youtube

par cris84 » 13 janv. 2015, 16:38

Bonjour à tous,
voilà je voulais récupérer les informations des vidéos de Youtube. J'ai le code ci-dessous, mais mise à part l'image rien d'autre n'est récupéré...
Quelqu'un pourrait-il m'aider en me disant s'il y a une ou plusieurs erreur dans mon code.

Merci d'avance... :D
<?php function getytid($url){

$yt_start = explode("v=",$url,2);
$yt_end = explode("&",$yt_start[1],2);
$gotid = $yt_end[0];
return $gotid;
}


/**
 * Gets Author from youtube id
 *
 * @param Youtube Id
 * @return $yt_author
 */
function getauthor($videoid){
$yt_xml_author_string = @file_get_contents("http://gdata.youtube.com/feeds/api/videos?q=".$videoid);
$yt_xml_author_start = explode("<uri>http://gdata.youtube.com/feeds/api/users/",$yt_xml_author_string,2);
$yt_xml_author_end = explode("</uri></author>",$yt_xml_author_start[1],2);
$yt_author = addslashes($yt_xml_author_end[0]);
return $yt_author;
}
function getdur($videoid){
$yt_xml_dur_string = @file_get_contents("http://gdata.youtube.com/feeds/api/videos?q=".$videoid);
$yt_xml_dur_start = explode("<yt:duration seconds='",$yt_xml_dur_string,2);
$yt_xml_dur_end = explode("'/>",$yt_xml_dur_start[1],2);
$yt_dur = addslashes($yt_xml_dur_end[0]);
return $yt_dur;
}
/**
 * Gets Title from youtube id
 *
 * @param Youtube Id
 * @return $yt_title_noslash
 */
function gettitle($videoid){
$yt_xml_title_string = @file_get_contents("http://gdata.youtube.com/feeds/api/videos?q=".$videoid);
$yt_xml_title_start = explode("<media:title type='plain'>",$yt_xml_title_string,2);
$yt_xml_title_end = explode("</media:title>",$yt_xml_title_start[1],2);
$yt_title = addslashes($yt_xml_title_end[0]);
$yt_title_noslash = $yt_xml_title_end[0];
return $yt_title_noslash;
}

/**
 * Gets description from youtube id
 *
 * @param Youtube Id
 * @return $yt_description
 */
function getdescription($videoid){
$yt_xml_description_string = @file_get_contents("http://gdata.youtube.com/feeds/api/videos?q=".$videoid);
$yt_xml_description_start = explode("<media:description type='plain'>",$yt_xml_description_string,2);
$yt_xml_description_end = explode("</media:description>",$yt_xml_description_start[1],2);
$yt_description = addslashes($yt_xml_description_end[0]);
return $yt_description;
} ?>
l'affichage de la vidéo :
<?php
if($_POST)
{
include 'conf.inc.php';
$videourl = $_POST['url'];
include 'youtube.php';

$videoid_untrim = getytid($videourl);

$videoid = trim($videoid_untrim);

		if($videoid !== null){
		$title  = gettitle($videoid);
		
		$author = getauthor($videoid);
		
		$des    = getdescription($videoid);
		
		$dur    = getdur($videoid);
		$temp = $dur % 3600;
$time[0] = ( $dur - $temp ) / 3600 ;
$time[2] = $temp % 60 ;
$time[1] = ( $temp - $time[2] ) / 60;
		$duration = $time[1].':'.$time[2];
		
		$thumb  = "http://img.youtube.com/vi/".$videoid."/1.jpg";

				}

$sql  = "SELECT COUNT(*) AS nbr FROM video WHERE url = '$videourl'";
    $res  = mysql_query($sql);
    $alors  = mysql_fetch_assoc($res);
         
      if(!($alors['nbr'] == 0)){
        $msg = "Cette vid&eacute;o est d&eacute;j&agrave; list&eacute; !";
      }else{

mysql_query("INSERT INTO video(url,title,description,author,thumb,duration) VALUES ('$videourl','$title','".mysql_real_escape_string($des)."','$author','$thumb','$duration')")or die(mysql_error());
$msg = 'Vid&eacute;o ajout&eacute;e';
}
}  


else { }

?>
<li class="box">
<img src="<?php echo $thumb; ?>" align="left"/>
<?php echo $title;?><br />
<?php echo stripslashes($des); ?><br />
Par : <b><?php echo $author; ?></b><br />
<br />Dur&eacute;e : <?php echo $duration; ?> min.
<br /><h1><?php echo $msg; ?></h1>
</li>