MyBB Community Forums

Full Version: Working with youtube api to get video data.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Actually i am working on this to get or retrieve information about videos from youtube, if i set normal urls i can fetch all entire data, but if i want to get information about that video from youtube with only the url information must be wrong.

I am using this code.

$youtubeRegexp = "/\s*[a-zA-Z\/\/:\.]*youtu(\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/si";
preg_match_all($youtubeRegexp, $text, $matches, PREG_SET_ORDER);
foreach ($matches as $m)
	$replace = get_video_data($m[2]);

$text = preg_replace($youtubeRegexp, $replace, $text);	

But this only parses latest video data and if i have more than one video the information does not be retrieved.

The function is this:

function get_video_data($id)
	$video_id = $id;
	$youtube_api_key = "Your_Api_Key";
		$url = "{$video_id}&key={$youtube_api_key}&part=snippet";
		$json = file_get_contents($url,0,null,null);
		$json_output = json_decode($json);
		foreach ( $json_output->items as $data )
			$video_id =  "[<a href=\"{$data->id}\">Link</a> <i class=\"fa fa-youtube\" style=\"color:red;\">&nbsp;</i>Youtube] [<b>Title</b> {$data->snippet->title}] ";
	return $video_id;	

This only keeps first video but if i have more than one how can i retrieve right information ?

Any sugestions ?

I am working on it but any help must be apreciated.
A while back I made a lazy-loader for YouTube videos for my site. I found the best way was to hook into parse_message_start and grab the info, globalize an array of replacements and then hook into parse_message and do the actual replacement.


$plugins->add_hook('parse_message_start', 'embedYouTubeStart');
function embedYouTubeStart($message)
	global $mybb, $ytVideos;

	$ytVideos = array();
	$pattern = '#(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:[^ |\n|\t]+)?#si';
	preg_match_all($pattern, $message, $links, PREG_SET_ORDER);

	foreach ((array) $links as $video) {
		$ytVideos[] = <<<EOF
{replacement here}
		$message = str_replace($video[0], '[YOUTUBE_VIDEO]', $message);
	return $message;

$plugins->add_hook('parse_message', 'rc_parse_message');
function embedYouTube($message)
	global $ytVideos;

	foreach ((array) $ytVideos as $replacement) {
		$message = preg_replace('#\[YOUTUBE_VIDEO\]#', $replacement, $message, 1);
	return $message;


That is kind of a stripped-down version of what I was doing, but maybe you can see what I mean.
Thanks @Wildcard that is more advanced and yep i was taken that on mind anyways at this point i have same results Sad

If i want it to parse like single file into my server side:

I have removed this code because it does not work as spected...

If i pass to my code also same code works into mybb app, but unfortunatelly if i use some other regexp do not work.

I have tryed many codes but uncommented line works fine but only first one video is parsed and at this point is the most approached results i want to get.

But if i use the second one (uncommented line) Actual way allways retrieve same results for both videos, anyway thanks to you i can use a limit to get right results.

And if i use the same limit to 1 in actual code anyways works fine, but i think there are some missing data into that var, so i prefer to use the commented code besides Smile

If i have more results latter i can rest, but i am so confused at this point :s
You can try using preg_replace_callback
Considering the API quota, it is better to not call the API inside a loop.
You can call it once, then iterate the data.
At least that's what I did in this simple YouTube video gallery:
I think is not necessary due i want it to do on text sent for now, maybe in next future i will make replacements, but no problem i will work on it this weekend, so i will remove my code because that code it does not work as i have thinked so i need to find a way to retrieve right information by scratch anyways.

Thanks for your time @RateU and @Wildcard
Well, if it is not necessary for you, then you can use the preg_replace_callback
Not really hard to do this though. It is just a suggestion Smile
Yep thanks i have used that before that was my firts headback but do not work with apis, only plain text.

For this i have used something like @Wildcard sugestion and until now it works fine Smile

I have thinked preg_replace_callback but unfortunatelly due it takes more than one url to get video informations, due it takes youtube links it is the same at the end by consulting and retrieving api queries.

But now it works so fine, i have added some excemptions to use only mycode youtube parsers if there is no api of if the api wont takes a response. This way allways match some information and a bypass must be created. And if api request is valid then the right information about that url must retrieved and additionally i have maded a sent request or live requests as an options Smile

But yeah, now all works as i expected and at my own needs, thanks for your suggestions anyway i really apreciate it buddies.
Glad you got it working Smile
Yeah thanks this is the final result at the end Smile

This is on an html page, but i will only get some data to show not all Smile

Anyways is nice to work with apis but not at all due constant upgrades and code changes, any new update you have to redo all entire code or at least the most important parts Smile