please help me to edit youtube APIs code

Discussion in 'PHP' started by crazy.works, Sep 19, 2008.

  1. #1
    Hello , I wanna make code to display the video thumbnail ,title ,author name and description . So i created code such as :-

    -------------------------------------------------------------------------------------------

    <?php
    // set feed URL
    $feedURL = 'http://gdata.youtube.com/feeds/api/videos/sZE2ZBKhXGc';

    // read feed into SimpleXML object
    $sxml = simplexml_load_file($feedURL);
    ?>
    <h1><?php echo $sxml->title; ?></h1><br>


    <?php
    // iterate over entries in feed
    foreach ($sxml->entry as $entry) {
    // get nodes in media: namespace for media information
    $media = $entry->children('http://search.yahoo.com/mrss/');

    // get video player URL
    $attrs = $media->group->player->attributes();
    $watch = $attrs['url'];

    // get video thumbnail
    $attrs = $media->group->thumbnail[0]->attributes();
    $thumbnail = $attrs['url'];
    ?>

    <div class="item">
    <span class="title">
    <a href="<?php echo $watch; ?>"><?php echo $media->group->title; ?></a>
    </span>
    <p><?php echo $media->group->description; ?></p>
    <p>
    <span class="thumbnail">
    <a href="<?php echo $watch; ?>"><img src="<?php echo $thumbnail;?>" /></a>
    <br/>click to view
    </span>
    <span class="attr">By:</span> <?php echo $entry->author->name; ?>
    </p>
    </div>

    <?php
    }
    ?>

    -------------------------------------------------------------------------------------------

    So if u tested the code on your browser ,you will find that :-

    1-this title query works <h1><?php echo $sxml->title; ?></h1><br>
    and the title has been displayed

    2- this other queries didn't work
    <?php echo $media->group->title; ?>
    <?php echo $media->group->description; ?>
    <?php echo $thumbnail;?>
    <?php echo $entry->author->name; ?>

    So why that data didn't displayed , what is the mistake in the code and what have i do to fix it ???

    Maybe somebody help me ??
     
    crazy.works, Sep 19, 2008 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    Using that xml parser is a mess, I prefer good old preg_match.
     
    Kaizoku, Sep 19, 2008 IP
  3. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    so if i wanna display the video, video thumbnail ,title ,author name and description with the old preg_match how the code will be look like ???

    beside i wish if anybody know how to fix my code
     
    crazy.works, Sep 19, 2008 IP
  4. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    SimpleXML was made for the purpose of parsing xml... and works much better than using regular expressions
     
    lui2603, Sep 19, 2008 IP
  5. crazy.works

    crazy.works Peon

    Messages:
    304
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    oh great , so if that code display the video title

    ----------------------------------------------------------------------
    <?php
    // set feed URL
    $feedURL = 'http://gdata.youtube.com/feeds/api/videos/sZE2ZBKhXGc';

    // read feed into SimpleXML object
    $sxml = simplexml_load_file($feedURL);
    ?>
    <h1><?php echo $sxml->title; ?></h1><br>

    -------------------------------------------------------------------------------------------

    how can i make it display only the video description too ????
     
    crazy.works, Sep 19, 2008 IP