Search code examples
phpxmlyoutubeyoutube-apigdata

Print out GData title in PHP


I have this link

http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1

That gives me the latest video from GudjoDaneel but I'd like to print our this title inside a PHP file

<title type='text'>The GD Project S3 | NEVER GIVE UP! | Division 1</title><content type='text'>

I'd appreciate it if someone could help me where to begin. And what I could look up.


Solution

  • I'd suggest looking up SimpleXML. It's easy to use once you get the hang of it, and you can get the title in just four line:

    $url    = 'http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1';
    $source = file_get_contents($url);
    $xml    = new SimpleXMLElement($source);
    $title  = $xml->entry->title;
    

    Do note, though, that $title is a PHP object in this case. If you echo it straight away, it'll be reinterpreted as a string, and everything will be alright. If you plan on doing anything else with it, you'll need to cast it as a string, like this:

    $title = strval($title);