K
K
Kairat Ibraev2020-11-11 14:28:11
YouTube
Kairat Ibraev, 2020-11-11 14:28:11

Is the simplexml_load_file(YOUTUBE_URL) function in php no longer relevant?

Is simplexml_load_file relevant in php to pull xml data from Youtube channel? There is deprecated code

<?php
  //Get videos from channel by YouTube Data API
  $ChannelID   = 'UCL8mk53VPSk5tKXixJq-FTA';

  define('YOUTUBE_URL', 'https://www.youtube.com/feeds/videos.xml?channel_id='. $ChannelID );//
// старого источника  
//define('YOUTUBE_URL', 'activity http://gdata.youtube.com/feeds/api/users/aliensabductedme/favorites');
  define('NUM_VIDEOS', 5);
  
 
  // Read the XML data into an object
 $xml = simplexml_load_file(YOUTUBE_URL);
  
  $num_videos_found = count($xml->entry);
  if ($num_videos_found > 0) {
    echo '<table><tr>';
    for ($i = 0; $i < min($num_videos_found, NUM_VIDEOS); $i++) {
      // Get the title
      $entry = $xml->entry[$i];
      $media = $entry->children('http://search.yahoo.com/mrss/');
      $title = $media->group->title;

      // Get the duration in minutes and seconds, and then format it
      $yt = $media->children('http://www.youtube.com/xml/schemas/2015');
      $attrs = $yt->duration->attributes();
      $length_min = floor($attrs['seconds'] / 60);
      $length_sec = $attrs['seconds'] % 60;
      $length_formatted = $length_min . (($length_min != 1) ? ' minutes, ':' minute, ') .
        $length_sec . (($length_sec != 1) ? ' seconds':' second');

      // Get the video URL
      $attrs = $media->group->player->attributes();
      $video_url = $attrs['url'];
 
      // Get the thumbnail image URL
      $attrs = $media->group->thumbnail[0]->attributes();
      $thumbnail_url = $attrs['url']; 

      // Display the results for this entry
      echo '<td style="vertical-align:bottom; text-align:center" width="' . (100 / NUM_VIDEOS) . '%"><a href="' . $video_url . '">' .
        $title . '<br /><span style="font-size:smaller">' . $length_formatted . '</span><br /><img src="' . $thumbnail_url . '" /></a></td>';
    }
    echo '</tr></table>';
  }
  else {
    echo '<p>Sorry, no videos were found.</p>';
  }
?>

but it doesn't work anymore . Everyone refers to YOUTUBE API V3 through a developer key and so on. but they don't explain well. But on such api, getting XML data is easy Gem**or. Who can suggest an alternative? Or sensibly explain how to do it in a human way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-11-11
@FanatPHP

No one can explain better than the documentation.
and there are examples too.
And no - programming is not about taking a ready-made codec, and if it doesn't work, then ask for another one.
We have to sit down and figure it out. If you have questions about specific issues that Google can't help with, then we'll be happy to help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question