web-mooc/app/Http/Controllers/CommonController.php
2026-01-12 08:05:36 +07:00

56 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use DateInterval;
use Illuminate\Http\Request;
class CommonController extends Controller
{
function get_video_details(Request $request, $url = "")
{
if ($url == "") {
$url = $request->url;
}
$host = explode('.', str_replace('www.', '', strtolower(parse_url($url, PHP_URL_HOST))));
$host = isset($host[0]) ? $host[0] : $host;
$youtube_api_key = get_settings('youtube_api_key');
if ($host == 'youtube' || $host == 'youtu') {
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$video_id = $match[1];
try {
$hash = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=$video_id&key=$youtube_api_key"));
} catch (\Throwable $th) {
$hash = '';
}
if ($hash == '') return;
$duration = new DateInterval($hash->items[0]->contentDetails->duration);
return array(
'provider' => 'YouTube',
'video_id' => $video_id,
'title' => $hash->items[0]->snippet->title,
'thumbnail' => 'https://i.ytimg.com/vi/' . $hash->items[0]->id . '/default.jpg',
'video' => "http://www.youtube.com/watch?v=" . $hash->items[0]->id,
'embed_video' => "http://www.youtube.com/embed/" . $hash->items[0]->id,
'duration' => $duration->format('%H:%I:%S'),
);
}
}
public function rendered_view(Request $request, $path = "")
{
$page_data = array();
foreach ($request->all() as $key => $value) :
$page_data[$key] = $value;
endforeach;
return view($path, $page_data)->render();
}
}