From 1e3cc2801835aa5d01cbd7f78fa6726020a7a728 Mon Sep 17 00:00:00 2001 From: ashwanipathak <39004850+ashwanipathak@users.noreply.github.com> Date: Sat, 5 May 2018 17:15:04 +0530 Subject: [PATCH] Parse Channel URL and get channel ID or username Added parseChannelIdFromUrl($url) function to parse URL and return the channel ID or username from the URL. --- src/Youtube.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Youtube.php b/src/Youtube.php index 6c1fe0c..dcc431e 100644 --- a/src/Youtube.php +++ b/src/Youtube.php @@ -376,6 +376,24 @@ public function getPlaylistsByChannelId($channelId, $optionalParams = array()) $apiData = $this->api_get($API_URL, $params); return $this->decodeList($apiData); } + + /** + * @param $url + * @return String + * @throws \Exception + */ + public function parseChannelIdFromUrl($url) { + + $url = parse_url($url); + $id = end(explode('/', $url['path'])); + + if (empty($id)) { + throw new \InvalidArgumentException('channel url is not correct.'); + } + + return $id; + } + /**