aboutsummaryrefslogtreecommitdiff
path: root/lib/Videolist/Platform/Youtube.php
blob: d5a388356d4b33f4134e23a26e48a44d71b83135 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

class Videolist_Platform_Youtube implements Videolist_PlatformInterface
{
    public function getType()
    {
        return "youtube";
    }

    public function parseUrl($url)
    {
        $parsed = parse_url($url);
        $id = '';
        if (! empty($parsed['host'])) {
            if ($parsed['host'] === 'youtu.be') {
                // short URLs
                $id = substr($parsed['path'], 1);
            } elseif ($parsed['host'] === 'www.youtube.com'
                    && $parsed['path'] === '/watch'
                    && ! empty($parsed['query'])) {
                // long URLs
                parse_str($parsed['query'], $query);
                if (! empty($query['v'])) {
                    $id = $query['v'];
                }
            }
        }
        if ($id) {
            return array(
                'video_id' => $id,
            );
        }
        return false;
    }

    public function getData($parsed)
    {
        $video_id = $parsed['video_id'];

        $buffer = file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$video_id);
        $xml = new SimpleXMLElement($buffer);

        return array(
            'title' => $xml->title,
            'description' => strip_tags($xml->content),
            'thumbnail' => "http://img.youtube.com/vi/$video_id/default.jpg",
        );
    }
}