0

I'm trying to migrate a youtube script to Youtube API version 2 There are certain videos that doesnt have a 3GP or MP4 mobile stream and with that I'm having

Fatal error: Call to a member function attributes() on a non-object in C:\wamp\www\view.php on line 29

Line 29 is

/* Get 3GP STREAM URL */
    $attrs = $media->group->content[1]->attributes();   /* THIS IS LINE 29 */
    $obj->tgpp = $attrs['url'];

I try to make a conditional statement to check if 3GP/MP4 stream link is available using empty and isset function

        if (!empty($media->group->content[1]->attributes())) {
        $attrs = $media->group->content[1]->attributes();
        $obj->tgpp = $attrs['url'];
    } else { 
        echo "";
    }

        if (isset($media->group->content[1]->attributes())) {
        $attrs = $media->group->content[1]->attributes();
        $obj->tgpp = $attrs['url'];
    } else { 
        echo "";
    }

both throw an error

Fatal error: Can't use method return value in write context in C:\wamp\www\view.php on line 29

with this snippet

        if ($media->group->content[1]->attributes() == "") { echo ""; }
    else {
    $attrs = $media->group->content[1]->attributes();
    $obj->tgpp = $attrs['url'];
    }

im back with an error of

Fatal error: Call to a member function attributes() on a non-object in C:\wamp\www\view.php on line 29

heres the script

        <?php
    header("Content-type: text/html; charset=UTF-8");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

    /* Function to parse a video <entry> */
    function parseVideoEntry($entry)  {
        $obj= new stdClass;
        /* Get author name and feed URL */
        $obj->author = $entry->author->name;
        $obj->authorURL = $entry->author->uri;

        /* Geet video publish date */
        $obj->publish = $entry->published;

        /* Get nodes in media: namespace for media information */
        $media = $entry->children('http://search.yahoo.com/mrss/');
        $obj->title = $media->group->title;
        $obj->description = $media->group->description;

        /* Get video category */
        $attrs = $media->group->category->attributes();
        $obj->category = $attrs['label'];

        /* Get FLV STREAM URL */
        $attrs = $media->group->content[0]->attributes();
        $obj->flv = $attrs['url'];

        /* Get 3GP STREAM URL */
        $attrs = $media->group->content[1]->attributes();
        $obj->tgpp = $attrs['url'];

        /* Get MP4 STREAM URL */
        $attrs = $media->group->content[2]->attributes();
        $obj->mp4 = $attrs['url'];

        /* Get video thumbnail */
        $attrs = $media->group->thumbnail[0]->attributes();
        $obj->thumbnailURL = $attrs['url'];  

        /* Get <yt:duration> node for video length */
        $yt = $media->children('http://gdata.youtube.com/schemas/2007');
        $attrs = $yt->duration->attributes();
        $obj->length = $attrs['seconds']; 

        /* Get <yt:stats> node for viewer statistics */
        $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
        if ($yt->statistics)
        {
            $attrs = $yt->statistics->attributes();
            $obj->viewCount = $attrs['viewCount']; 
        }
        else
        {
            $obj->viewCount = 0;
        }

        //LIKES get <yt:rating> node for number of likes statistics
        //$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
        if ($yt->rating)
        {
            $attrs = $yt->rating->attributes();
            $obj->numLikes = $attrs['numLikes']; 
        }
        else
        {
            $obj->numLikes = 0; 
        }

        //DISLIKES get <yt:rating> node for number of dislikes statistics
        //$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
        if ($yt->rating) 
        {       
            $attrs = $yt->rating->attributes();
            $obj->numDislikes = $attrs['numDislikes'];
        } else 
        {
            $obj->numDislikes = 0; 
        }
        return $obj;
    } // close funcion parseVideoEntry




        /* Get video ID from $_GET  */
        !isset($_GET['id']) ? die ('ERROR: Missing video ID') : $vid = $_GET['id'];

        /* Set video data feed URL */
        $feedURL = 'http://gdata.youtube.com/feeds/api/videos/'.$vid.'?v=2';

        /* Read feed into SimpleXML object */
        $entry = simplexml_load_file($feedURL);

        /* Parse video entry */
         $video = parseVideoEntry($entry);       
    ?>
        <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <title><?=$video->title?></title>
        <link href="./style.css" rel="stylesheet" type="text/css" />
        </head>
        <ul>
            <li>Stream:</li>
            <li><a href="<?=$video->tgpp?>">3GP</a></li>
            <li><a href="<?=$video->mp4?>">MP4</a></li>
            <li><a href="<?=$video->flv?>">Youtube Player</a></li>
        </ul>
4

1 回答 1

0
if(isset($media) and isset($media->group) and isset($media->group->content[1])) {
    $attrs = $media->group->content[1]->attributes();
    $obj->tgpp = $attrs['url'];
}
else {
...
于 2013-07-09T05:33:20.440 回答