Intégration de l'API de Youtube


yule
WRInaute discret
WRInaute discret
 
Messages: 197
Inscription: 23 Juil 2004

Intégration de l'API de Youtube

Message le Jeu Juin 21, 2007 16:50

Bonjour,

J'ai voulu intégrer le module assez complet de l'API de youtube par cet exemple (fichier zip en bas de page...)

http://waxjelly.wordpress.com/2007/03/0 ... s-redeaux/

Je l'ai installé et testé .... mais malheureusement j'ai un message d'erreur... pour vous c'est la même chose ?

Message d'erreur :
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home.10.15/xxxxxx/www/youtube/youtube.class.php on line 36

D'avance merci pour votre avis...

Cordialement
Yule

lefou
WRInaute discret
WRInaute discret
 
Messages: 131
Inscription: 7 Juin 2005

Message le Jeu Juin 21, 2007 18:02

Un peu de code et ça irait mieux pour t'apporter une aide. Apparemment vérifie s'il ne manque pas quelquepart une } ou { ou ;


yule
WRInaute discret
WRInaute discret
 
Messages: 197
Inscription: 23 Juil 2004

Message le Jeu Juin 21, 2007 18:57

Re,

Oups.... c'est vrai que cela pourrait aider....

la ligne 36 est la suivante

36 public


Code: Tout sélectionner
<?php
error_reporting(E_ALL);

// for pagination
session_start();

////////////////////////////////////////
// youtube.class.php v1.0 /////////
// released 3/8/2007 ///////////////
// for waxjelly.wordpress.com ////
// the internet is for users ////////
////////////////////////////////////////
// this class is to be used free ////
// of charge so long as this line //
// remains intact, and untouched/
////////////////////////////////////////
define('API_KEY', 'xxxxxxxx'); // YOUR YOUTUBE DEVELOPERS API KEY
//-------------------------------//
// DO NOT EDIT
//-------------------------------//
/////////////////////
// CONSTANTS //
////////////////////
define('API_URL', 'http://www.youtube.com/');

// CALL GATEWAYS
define('API_REST_CALL', API_URL . 'api2_rest?');
define('API_XMLRPC_CALL', API_URL . 'api2_xmlrpc?');

// METHODS FOR VIDEOS
define('BY_TAG', 'list_by_tag');

// create youtube class
class youtube {
   // CLASS PROPERTIES
   public
   // VARIABLE TO HOLD THE SIMPLE XML ELEMENT WITH RETURNED DATA
   $xml,
   // HOLDS CONFIG OPTIONS
   $config = array(),
   // VAR TO STORE THE QUERY STRING FOR YOUTUBE
   $api_call,
   // RETURNED DATA FROM XML CALL
   $return,
   $tag,
   // IF WE ENCOUNTER ERRORS ALONG OUR JOURNEY
   $errors;

   // CONSTRUCTION VARIABLES
   public function __construct($api_key = API_KEY, $page = 1, $per_page = 25) {
      // JUST TO SETUP SOME DEFAULT VALUES FOR A NOMINAL CALL.
      // WE WANTED TO MAKE IT EASY TO JUST USE THE CLASS, NOT CONFIGURE IT.
      youtube::set('api_key', API_KEY);
      youtube::set('page', $page);
      youtube::set('per_page', $per_page);

      // modify the call
      youtube::modify_call(API_REST_CALL . 'method=youtube.');
   }

MichaelB
Nouveau WRInaute
Nouveau WRInaute
 
Messages: 13
Inscription: 29 Jan 2006

Message le Jeu Juin 21, 2007 20:39

Hello,

Il te manque un } tout en bas de ton fichier pour fermer ta class..


yule
WRInaute discret
WRInaute discret
 
Messages: 197
Inscription: 23 Juil 2004

Message le Jeu Juin 21, 2007 21:57

Hello,

Merci le neuneu... (du bas ou du haut...)

Selon les commentaires du site officiel, il semblerait que ce script ne fonctionne qu'avec php5... et non php4

Je vais prendre mon mal en patience... d'ici une version pour php4

Cordialement
Yule

bigjet
WRInaute discret
WRInaute discret
 
Messages: 211
Inscription: 21 Nov 2004

Message le Jeu Juin 21, 2007 23:46

Le contenu de youtube est fourni sous format XML, donc tu peux facilement l'utiliser avec PHP4.

Voici une fonction pour convertir du XML dans un tableau php:

Code: Tout sélectionner
function xml2array($contents, $get_attributes=1) {
    if(!$contents) return array();

    if(!function_exists('xml_parser_create')) {
        //print "'xml_parser_create()' function not found!";
        return array();
    }
    //Get the XML parser of PHP - PHP must have this module for the parser to work
    $parser = xml_parser_create();
    xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
    xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
    xml_parse_into_struct( $parser, $contents, $xml_values );
    xml_parser_free( $parser );

    if(!$xml_values) return;//Hmm...

    //Initializations
    $xml_array = array();
    $parents = array();
    $opened_tags = array();
    $arr = array();

    $current = &$xml_array;

    //Go through the tags.
    foreach($xml_values as $data) {
        unset($attributes,$value);//Remove existing values, or there will be trouble
        extract($data);//We could use the array by itself, but this cooler.

        $result = '';
        if($get_attributes) {//The second argument of the function decides this.
            $result = array();
            if(isset($value)) $result['value'] = $value;

            //Set the attributes too.
            if(isset($attributes)) {
                foreach($attributes as $attr => $val) {
                    if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
                    /**  :TODO: should we change the key name to '_attr'? Someone may use the tagname 'attr'. Same goes for 'value' too */
                }
            }
        } elseif(isset($value)) {
            $result = $value;
        }

        //See tag status and do the needed.
        if($type == "open") {//The starting of the tag '<tag>'
            $parent[$level-1] = &$current;

            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
                $current[$tag] = $result;
                $current = &$current[$tag];

            } else { //There was another element with the same tag name
                if(isset($current[$tag][0])) {
                    array_push($current[$tag], $result);
                } else {
                    $current[$tag] = array($current[$tag],$result);
                }
                $last = count($current[$tag]) - 1;
                $current = &$current[$tag][$last];
            }

        } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
            //See if the key is already taken.
            if(!isset($current[$tag])) { //New Key
                $current[$tag] = $result;

            } else { //If taken, put all things inside a list(array)
                if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
                        or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) {
                    array_push($current[$tag],$result); // ...push the new element into that array.
                } else { //If it is not an array...
                    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
                }
            }

        } elseif($type == 'close') { //End of tag '</tag>'
            $current = &$parent[$level-1];
        }
    }

    return($xml_array);
}



Par exemple, pour retourner les 10 dernières vidéos contenant un mot clé spécifiqueL
Code: Tout sélectionner
$tab = xml2array(@file_get_contents("http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag&dev_id=Ta clé api&tag=TON MOT CLÉ&page=1&per_page=10"), 1);



Fais ensuite un print_r($tab) pour voir la structure de ton tableau php.

swat
Nouveau WRInaute
Nouveau WRInaute
 
Messages: 2
Inscription: 13 Oct 2007

Message le Sam Oct 13, 2007 1:46

Bonjour,

Comment pui-je avoir mon API_KEY

Code: Tout sélectionner
nclude the youtube class and instantiate it with the object $yt

    include(’youtube.class.php’);
    $yt = new youtube(’YOUR_API_KEY’);


Si vous pensez que je serai bloqué sur une étape prochaine, merci de votre aide :D

Cheers


Si vous avez aimé cette discussion, partagez-la sur vos réseaux sociaux préférés :

Lectures recommandées sur ce thème :



Qui est en ligne

Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 1 invité