2018-02-08, 01:29 AM
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.
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:
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.
I am using this code.
$youtubeRegexp = "/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/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";
if(isset($youtube_api_key))
{
$url = "https://www.googleapis.com/youtube/v3/videos?id={$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=\"https://www.youtube.com/watch?v={$data->id}\">Link</a> <i class=\"fa fa-youtube\" style=\"color:red;\"> </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.