2

Im trying to use PHP's SimpleXMLElement to parse a YouTube channel feed and include the description. I can do this easily to get the title and video url.

Here is what I have that can get the descriptions

$channel_id = 'UCtNjkMLQQOX251hjGqimx2w';
$yt_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=';
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$result = $xml->xpath("//media:description");
foreach($result as $r){
 print $r;
 print '<br />';
}

The Raw xml from YouTube looks something like this.

 <entry>
  <id>yt:video:OTYFJaT-Glk</id>
  <yt:videoId>OTYFJaT-Glk</yt:videoId>
  <yt:channelId>UCtNjkMLQQOX251hjGqimx2w</yt:channelId>
  <title>Guitar E.R. - Setting up wood shop for O Positive custom speaker cabinets.</title>
  <link rel="alternate" href="https://www.youtube.com/watch?v=OTYFJaT-Glk"/>
  <author>
   <name>Guitar ER</name>
   <uri>https://www.youtube.com/channel/UCtNjkMLQQOX251hjGqimx2w</uri>
  </author>
  <published>2020-10-18T16:38:51+00:00</published>
  <updated>2020-10-20T01:04:59+00:00</updated>
  <media:group>
   <media:title>Guitar E.R. - Setting up wood shop for O Positive custom speaker cabinets.</media:title>
   <media:content url="https://www.youtube.com/v/OTYFJaT-Glk?version=3" type="application/x-shockwave-flash" width="640" height="390"/>
   <media:thumbnail url="https://i4.ytimg.com/vi/OTYFJaT-Glk/hqdefault.jpg" width="480" height="360"/>
   <media:description>In this video Doctor John Moran of Guitar E.R. gives us a glimpse into the new wood shop where he will be building custom guitar and bass cabinets, likely under the name O Positive Cabinets.</media:description>
   <media:community>
    <media:starRating count="0" average="0.00" min="1" max="5"/>
    <media:statistics views="22"/>
   </media:community>
  </media:group>
 </entry>
 <entry>

As you can see the title and uri and pretty easy to get, but I'm having trouble combining my code to get the Title, URI and Description.

Here is my code to get the Title and URI

$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
foreach($xml->entry as $entry){
    print $entry->title;
    print '<br />';
    print $entry->author->uri;
    print '<br />';
}

After working on this for awhile I was able to come up with a solution, not sure if this is the best answer though.

<?
$channel_id = 'UCtNjkMLQQOX251hjGqimx2w';
$yt_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=';

$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');

$yt_data = array();
foreach($xml->entry as $entry){

    $videoid = (string)$entry->children('yt', true)->videoId;
    $yt_data[] = array(
        'id' => $videoid,
        'title' => (string)$entry->title,
        'description' => (string)$entry->children('media', true)->group->description,
        'img' => (string)'https://img.youtube.com/vi/'.$videoid.'/maxresdefault.jpg',
        'thumb' => (string)'https://img.youtube.com/vi/'.$videoid.'/mqdefault.jpg',
        'published' => (string)$entry->published,
        'updated' => (string)$entry->updated,
        'channel' => (string)$entry->children('yt', true)->channelId,
        'author' => (string)$entry->author->name,
        'uri' => (string)$entry->author->uri,
        'views' => (string)$entry->children('media', true)->group->community->statistics->attributes()['views'],
        'ratings_count' => (string)$entry->children('media', true)->group->community->starRating->attributes()['count'],
        'ratings_avg' => (string)$entry->children('media', true)->group->community->starRating->attributes()['average'],
    );
    
}

foreach($yt_data as $yt){
    print '<div class="ytVidWrap">';
    print '<a href="https://www.youtube.com/watch?v='.$yt['id'].'" target="_blank">';
    print '<img src="'.$yt['thumb'].'" alt="YouTube Video" />';
    print '<h1>'.$yt['title'].'</h1>';
    print '<p>'.$yt['description'].'</p>';
    print '<p>'.$yt['views'].' views &bull; '.date('M j, Y',strtotime($yt['published'])).'</p>';
    print '</a>';
    print '</div>';
}
?>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.