请帮我编写一个php文件,它的功能是检测网页中的视频。通过?url=获取一个网址,然后检查网址对应的页面中是否有视频文件,如果有,则以json格式返回m3u8或其它格式的视频链接;没有的话,也要返回提示信息。返回的json中要有code、视频标题title、视频类型type、视频链接url这4个参数
时间: 2023-03-13 12:12:10 浏览: 61
很高兴为您服务!下面是我为您编写的php文件:<?php
// 获取url
$url = $_GET['url'];// 初始化curl
$ch = curl_init($url);
// 强制获取视频
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);// 执行curl
$content = curl_exec($ch);// 解析页面
$doc = new DOMDocument();
@$doc->loadHTML($content);$metas = $doc->getElementsByTagName('meta');$video_url = '';
$video_type = '';
$video_title = '';for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('property') == 'og:video')
{
$video_url = $meta->getAttribute('content'); // 获取视频类型
$video_type = substr($video_url, strrpos($video_url, '.') + 1); // 获取视频标题
$video_title = $meta->getAttribute('title');
}
}// 返回json
$return_array = array();
if(!empty($video_url))
{
$return_array['code'] = 1;
$return_array['title'] = $video_title;
$return_array['type'] = $video_type;
$return_array['url'] = $video_url;
}
else
{
$return_array['code'] = 0;
$return_array['title'] = 'Not Found';
$return_array['type'] = 'null';
$return_array['url'] = 'null';
}echo json_encode($return_array);?>