Skip to content

Commit

Permalink
Merge pull request #38 from kalimba/master
Browse files Browse the repository at this point in the history
Use the second parameters of parse_url function to determinate url parts.
  • Loading branch information
madcoda committed Jul 14, 2015
2 parents b1beb5b + 979cad6 commit f6e3bf4
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions lib/Madcoda/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,25 @@ public function getActivitiesByChannelId($channelId)
*/
public static function parseVIdFromURL($youtube_url)
{
$videoId = null;
if (strpos($youtube_url, 'youtube.com')) {
$params = static::_parse_url_query($youtube_url);
return $params['v'];
if (strpos($youtube_url, 'embed')) {
$path = static::_parse_url_path($youtube_url);
$videoId = substr($path, 7);
}
if($params = static::_parse_url_query($youtube_url)) {
$videoId = isset($params['v']) ? $params['v'] : null;
}
} else if (strpos($youtube_url, 'youtu.be')) {
$path = static::_parse_url_path($youtube_url);
$vid = substr($path, 1);
return $vid;
} else {
$videoId = substr($path, 1);
}

if (empty($videoId)) {
throw new \Exception('The supplied URL does not look like a Youtube URL');
}

return $videoId;
}

/**
Expand Down Expand Up @@ -485,8 +494,7 @@ public function api_get($url, $params)
*/
public static function _parse_url_path($url)
{
$array = parse_url($url);
return $array['path'];
return parse_url($url, PHP_URL_PATH);
}

/**
Expand All @@ -497,16 +505,16 @@ public static function _parse_url_path($url)
*/
public static function _parse_url_query($url)
{
$array = parse_url($url);
$query = $array['query'];

$queryParts = explode('&', $query);
$queryString = parse_url($url, PHP_URL_QUERY);

$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = empty($item[1]) ? '' : $item[1];

parse_str($queryString, $params);

if (is_null($params)) {
return array();
}
return $params;

return array_filter($params);
}
}

0 comments on commit f6e3bf4

Please sign in to comment.