@@ -64,10 +64,30 @@ public function getVideoDetail($id)
6464 'like_count ' => isset ($ data ->items [0 ]->statistics ->likeCount ) ? $ data ->items [0 ]->statistics ->likeCount : 0 ,
6565 'view_count ' => isset ($ data ->items [0 ]->statistics ->viewCount ) ? $ data ->items [0 ]->statistics ->viewCount : 0 ,
6666 'comment_count ' => isset ($ data ->items [0 ]->statistics ->commentCount ) ? $ data ->items [0 ]->statistics ->commentCount : 0 ,
67- 'uploader ' => null
67+ 'uploader ' => $ data -> items [ 0 ]-> snippet -> channelTitle
6868 );
6969 }
7070
71+ /**
72+ * Get Video Raw Data
73+ * @param string $id
74+ * @return array
75+ * @throws \Exception
76+ */
77+ public function getVideoRawData ($ id )
78+ {
79+ $ this ->setId ($ id );
80+
81+ $ data = $ this ->getData (str_replace ('{key} ' , $ this ->key , $ this ->baseVideoUrl ));
82+
83+ if (isset ($ data ->error ))
84+ {
85+ throw new \Exception ("Video not found " );
86+ }
87+
88+ return $ data ;
89+ }
90+
7191 /**
7292 * Get Video List
7393 * @param string $id
@@ -93,4 +113,71 @@ public function getVideoList($id)
93113
94114 return $ list ;
95115 }
116+
117+ /**
118+ * Parse a youtube URL to get the youtube Vid.
119+ * Support both full URL (www.youtube.com) and short URL (youtu.be)
120+ * Source: https://github.com/alaouy/Youtube
121+ *
122+ * @param string $youtube_url
123+ * @throws \Exception
124+ * @return string Video Id
125+ */
126+ public function parseVIdFromURL ($ youtube_url )
127+ {
128+ if (strpos ($ youtube_url , 'youtube.com ' )) {
129+ if (strpos ($ youtube_url , 'embed ' )) {
130+ $ path = static ::_parse_url_path ($ youtube_url );
131+ $ vid = substr ($ path , 7 );
132+ return $ vid ;
133+ } else {
134+ $ params = static ::_parse_url_query ($ youtube_url );
135+ return $ params ['v ' ];
136+ }
137+ } else if (strpos ($ youtube_url , 'youtu.be ' )) {
138+ $ path = static ::_parse_url_path ($ youtube_url );
139+ $ vid = substr ($ path , 1 );
140+ return $ vid ;
141+ } else {
142+ throw new \Exception ('The supplied URL does not look like a Youtube URL ' );
143+ }
144+ }
145+
146+ /**
147+ * Parse the input url string and return just the path part
148+ * Source: https://github.com/alaouy/Youtube
149+ *
150+ * @param string $url the URL
151+ * @return string the path string
152+ */
153+ public static function _parse_url_path ($ url )
154+ {
155+ $ array = parse_url ($ url );
156+
157+ return $ array ['path ' ];
158+ }
159+
160+ /**
161+ * Parse the input url string and return an array of query params
162+ * Source: https://github.com/alaouy/Youtube
163+ *
164+ * @param string $url the URL
165+ * @return array array of query params
166+ */
167+ public static function _parse_url_query ($ url )
168+ {
169+ $ array = parse_url ($ url );
170+ $ query = $ array ['query ' ];
171+
172+ $ queryParts = explode ('& ' , $ query );
173+
174+ $ params = array ();
175+ foreach ($ queryParts as $ param ) {
176+ $ item = explode ('= ' , $ param );
177+ $ params [$ item [0 ]] = empty ($ item [1 ]) ? '' : $ item [1 ];
178+ }
179+
180+ return $ params ;
181+ }
96182}
183+
0 commit comments