-1

I keep getting this error in line 54 and I don't know why. All that I'm trying to do is to process the first entry of the query result: extract it's URL so as to construct an embeddable video object.

<?php
    function extractID($youtubeURL)
    {
        //split off the final bit of the URL beginning with �?=’
        $youtubeID = strchr($youtubeURL,'=');
        //remove that equals sign to get the video ID 
        $youtubeID = substr($youtubeID,1);
        return $youtubeID;
    }
?>
<?php
    // set feed URL
    $lang = $_POST['language'];
    $query = $_POST['query'] . "%20review";
    switch($lang){
        case "English":
            $feedURL = <<<URL
            http://gdata.youtube.com/feeds/api/videos
            ?q=<?php echo $query; ?>
            &v=2
            &format=5
            &lr=en
            URL;
            break;
        case "French":
            $feedURL = <<<URL
            http://gdata.youtube.com/feeds/api/videos
            ?q=<?php echo $query; ?>
            &v=2
            &format=5
            &lr=fr
            URL;
            break;
        case "Spanish":
            $feedURL = <<<URL
            http://gdata.youtube.com/feeds/api/videos
            ?q=<?php echo $query; ?>
            &v=2
            &format=5
            &lr=es
            URL;
            break;                    
    }

    // read feed into SimpleXML object
    $sxml = simplexml_load_file($feedURL);

    // get the first media entry &
   // get nodes in media: namespace for media information
   $media = $sxml->entry[0]->children('http://search.yahoo.com/mrss/');

   // get video player URL
   $attrs = $media->group->player->attributes();
   // **THIS KEEPS CAUSING THE ERROR.**
   $videoURL = $attrs['url'];
   // extract the video's ID from the URL
   $videoID = extractID($videoURL);       
?>

<?php
    echo <<<EOD 
    <objectwidth="425" height="350" data="http://www.youtube.com/v/
    <?php echo $videoID ?> 
    type="application/x-shockwave-flash"><param name="src" 
    value="http:/www.youtube.com/v/<?php echo $videoID ?>" /></object>
    EOD;
?>
2
  • Looks like a PHP syntax error, not a Youtube error... Commented May 8, 2009 at 21:05
  • Thanks for your contribution. I think you're right. The question is: where is the syntax error? Commented May 8, 2009 at 21:22

2 Answers 2

2

The problem in the PHP code you've presented appears to be in the part of it that constructs the YouTube RSS feed URL. It looks like the following:

<?php
// set feed URL
$lang = $_POST['language'];
$query = $_POST['query'] . "%20review";
switch($lang){
    case "English":
        $feedURL = <<<URL
        http://gdata.youtube.com/feeds/api/videos
        ?q=<?php echo $query; ?>
        &v=2
        &format=5
        &lr=en
        URL;
        break;
    case "French":
        $feedURL = <<<URL
        http://gdata.youtube.com/feeds/api/videos
        ?q=<?php echo $query; ?>
        &v=2
        &format=5
        &lr=fr
        URL;
        break;
    case "Spanish":
        $feedURL = <<<URL
        http://gdata.youtube.com/feeds/api/videos
        ?q=$query;
        &v=2
        &format=5
        &lr=es
        URL;
        break;
}

There are two things that I can see are wrong with this:

  1. The URL; lines at the end of the heredocs should not be indented. They should look something like the following:

        case "English":
            $feedURL = <<<URL
            http://gdata.youtube.com/feeds/api/videos
            ?q=<?php echo $query; ?>
            &v=2
            &format=5
            &lr=en
    URL;
            break;
    
  2. Don't use <?php echo $query; ?> within heredocs. Just use $query on its own, as in the case "Spanish": block above.

After fixing these heredocs, you'll find that the code won't do what you want. $feedURL will contain a lot of unwanted whitespace, and this will cause your call to simplexml_load_file to fail. But if you look closely at the feed URLs, you'll notice that they are all the same apart from the 'language code' (en, fr, es) at the end. What I would do would be to use an array that translates language names into language codes, and use that to append the language code to the feed URL. For example, something like the following should work:

$lang_codes = array(
    "English" => "en",
    "French"  => "fr",
    "Spanish" => "es"
);

$feedURL = "http://gdata.youtube.com/feeds/api/videos?q=$&v=2&format=5&lr=" . $lang_codes[$lang];

The rest of your code appears to work. Once I had fixed up getting the RSS feed URL, it quite happily fetched the feed, pulled a YouTube URL out of it and got the video ID.

Sign up to request clarification or add additional context in comments.

Comments

-2

The problem is that $media->group is empty. It has no children. That means that $media->group->player is NULL. You cannot call attributes() on NULL so you get a syntax error.

2 Comments

Thanks for your answer. One more question: how do I ensure that $media->group is non-empty?
-1: this is not the problem at all. Attempting to call attributes() on NULL will give you a runtime error, never a syntax error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.