acctman
acctman

Reputation: 4349

YouTube parse video id from links

Using a Youtube Channel how do I parse the $link variable and get the video ID? I can get the links but the parse_str part doesn't seem to be working properly

youtube id parsing: trying to get to work with current code below

if ( ! empty($xml->channel->item[0]->link) )
{
  parse_str(parse_url($xml->channel->item[0]->link, PHP_URL_QUERY), $url_query);

  if ( ! empty($url_query['v']) )
    $id = $url_query['v'];
}

echo $id;

current code:

    <?php
    $id = NULL;
    $username = 'utahblaze';
    $url = 'http://gdata.youtube.com/feeds/api/users/%s/uploads?orderby=updated&max-results=8';
    $xml = simplexml_load_file(sprintf($url, $username));

    foreach ($xml->entry as $entry) :

        $kids = $entry->children('http://search.yahoo.com/mrss/');
        $attributes = $kids->group->content[0]->attributes();
        $flv = $attributes['url'];
        $attributes = $kids->group->player->attributes();
        $link = $attributes['url']; 


    echo $id;
    echo $link;

    ?>

<a id="ytopen" href="<?=$link?>">
<img src="http://i4.ytimg.com/vi/<?=$id?>/default.jpg" /></a>

    <?php endforeach; ?>

Upvotes: 1

Views: 716

Answers (1)

seanbreeden
seanbreeden

Reputation: 6097

Put this after $link = $attributes['url'];

$querystring = parse_url($link);
$id = $querystring['query'];
$path = $querystring['path'];
$host = $querystring['host'];
$scheme = $querystring['scheme'];

parse_str($querystring['query'], $id_temp);

$link = $scheme . '://' . $host . $path . '/' . $id_temp['v'];

Upvotes: 2

Related Questions