Alaouy\Youtube\Youtube::getChannelFromURL PHP Method

getChannelFromURL() public method

Get the channel object by supplying the URL of the channel page
public getChannelFromURL ( string $youtube_url ) : object
$youtube_url string
return object Channel object
    public function getChannelFromURL($youtube_url)
    {
        if (strpos($youtube_url, 'youtube.com') === false) {
            throw new \Exception('The supplied URL does not look like a Youtube URL');
        }
        $path = static::_parse_url_path($youtube_url);
        if (strpos($path, '/channel') === 0) {
            $segments = explode('/', $path);
            $channelId = $segments[count($segments) - 1];
            $channel = $this->getChannelById($channelId);
        } else {
            if (strpos($path, '/user') === 0) {
                $segments = explode('/', $path);
                $username = $segments[count($segments) - 1];
                $channel = $this->getChannelByName($username);
            } else {
                throw new \Exception('The supplied URL does not look like a Youtube Channel URL');
            }
        }
        return $channel;
    }

Usage Example

Ejemplo n.º 1
0
 public function testGetChannelFromURL()
 {
     $channel = $this->youtube->getChannelFromURL('http://www.youtube.com/user/Google');
     $this->assertEquals('UCK8sQmJBp8GCxrOtXWBpyEA', $channel->id);
     $this->assertEquals('Google', $channel->snippet->title);
 }