Skip to content

Commit f6e3bf4

Browse files
committed
Merge pull request #38 from kalimba/master
Use the second parameters of parse_url function to determinate url parts.
2 parents b1beb5b + 979cad6 commit f6e3bf4

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

lib/Madcoda/Youtube.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,25 @@ public function getActivitiesByChannelId($channelId)
323323
*/
324324
public static function parseVIdFromURL($youtube_url)
325325
{
326+
$videoId = null;
326327
if (strpos($youtube_url, 'youtube.com')) {
327-
$params = static::_parse_url_query($youtube_url);
328-
return $params['v'];
328+
if (strpos($youtube_url, 'embed')) {
329+
$path = static::_parse_url_path($youtube_url);
330+
$videoId = substr($path, 7);
331+
}
332+
if($params = static::_parse_url_query($youtube_url)) {
333+
$videoId = isset($params['v']) ? $params['v'] : null;
334+
}
329335
} else if (strpos($youtube_url, 'youtu.be')) {
330336
$path = static::_parse_url_path($youtube_url);
331-
$vid = substr($path, 1);
332-
return $vid;
333-
} else {
337+
$videoId = substr($path, 1);
338+
}
339+
340+
if (empty($videoId)) {
334341
throw new \Exception('The supplied URL does not look like a Youtube URL');
335342
}
343+
344+
return $videoId;
336345
}
337346

338347
/**
@@ -485,8 +494,7 @@ public function api_get($url, $params)
485494
*/
486495
public static function _parse_url_path($url)
487496
{
488-
$array = parse_url($url);
489-
return $array['path'];
497+
return parse_url($url, PHP_URL_PATH);
490498
}
491499

492500
/**
@@ -497,16 +505,16 @@ public static function _parse_url_path($url)
497505
*/
498506
public static function _parse_url_query($url)
499507
{
500-
$array = parse_url($url);
501-
$query = $array['query'];
502-
503-
$queryParts = explode('&', $query);
508+
$queryString = parse_url($url, PHP_URL_QUERY);
504509

505510
$params = array();
506-
foreach ($queryParts as $param) {
507-
$item = explode('=', $param);
508-
$params[$item[0]] = empty($item[1]) ? '' : $item[1];
511+
512+
parse_str($queryString, $params);
513+
514+
if (is_null($params)) {
515+
return array();
509516
}
510-
return $params;
517+
518+
return array_filter($params);
511519
}
512520
}

0 commit comments

Comments
 (0)