1. 首页
  2. 服务器

视频地址解析方法

 

这个方法是视频解析的方法,弄了两天终于成功了,使用php在后台解析某视频平台的视频并进行引用。 演示地址:https://jiayu.mybabya.com/post/5368.html

一、先模拟浏览器User-Agent
因为很多网站对浏览用户是做了反爬虫过滤的,所以我们需要先对我们的post和get数据进行模拟浏览器行为。我这里把常用的浏览器User-Agent做成了一个随机模块。

/*
* 随机User-Agent浏览器标识模块
* */
function useragent_code()
{
$b = rand(1, 10);
if ($b == 1) {
$ua = "Mozilla/5.0(Macintosh;U;IntelMacOSX10_6_8;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/.1Safari/534.50";
}
if ($b == 2) {
$ua = "Mozilla/5.0(Windows;U;WindowsNT6.;en-us)AppleWebKit/534.50(KHTML,likeGecko)Version/.1Safari/534.50";
}
if ($b == 3) {
$ua = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0";
}
if ($b == 4) {
$ua = "Opera/9.80(Macintosh;IntelMacOSX10.6.8;U;en)Presto/2.8.131Version/11.11";
}
if ($b == 5) {
$ua = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0";
}
if ($b == 6) {
$ua = "Opera/9.80(WindowsNT6.;U;en)Presto/2.8.131Version/11.11";
}
if ($b == 7) {
$ua = "Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0..56Safari/535.11";
}
if ($b == 8) {
$ua = "Mozilla/5.0(iPhone;U;CPUiPhoneOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.";
}
if ($b == 9) {
$ua = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
}
if ($b == 10) {
$ua = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
}
return $ua;
}

二、模拟浏览器header文件头,因为要过反爬虫机制,所以必须要模拟文件头,意思是模拟一个正常用户浏览的行为。获取网站文件头的方法可以使用浏览器中的F12键调出开发者工具,在“网络”块,找到这个用户的首页地址,单击并查看右侧的“消息头”,在这里有协议,header文件头,cookie等信息,填写下方这代码里就可以了。

/**
* 模拟浏览器开始访问请求
* @param $url
* @return bool|string
*/
function fetch_url($url)
{
$header = video_code($url);
$timeout = 120;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
//设置请求头信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//不取得返回头信息
curl_setopt($ch, CURLOPT_HEADER, 0);
// 关闭https验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_USERAGENT, useragent_code());
$content = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
return $content;
}
curl_close($ch);
}
/*
* 视频平台的文件头
**/
function video_code($url)
{
if (stripos($url, "toutiao", 0) !== false) {
$iurl = str_replace("替换1", "替换2", $url);
$iurl = str_replace("/a", "/", $iurl);
$url = $iurl;
}
if (stripos($url, "?wid_try=1", 0) !== false || stripos($url, "?", 0) !== false) {
$web = text_capture($url, "https://", "/"); //获取 用于HOST
$id = text_capture($url, "m/", "?"); // 获取链接ID,用于 filename
$ic = stripos($url, "?", 0);
$ii = substr($url, 0, $ic) . "?wid_try=1"; //获取完整链接,用于Referer
} else {
$web = text_capture($url, "https://", "/"); // 用于HOST
$id = text_capture($url, "m/", "?"); // 获取链接ID,用于 filename
$ii = $url . "?wid_try=1";
}
$header = array(
"POST {$ii} HTTP/2.0",
"Host: {$web} ",
"filename: {$id} ",
"Referer: {$ii} ",
"Content-Type: text/html",
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding:gzip, deflate, br',
'Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection:keep-alive',
'Cookie: *******************************',
'User-Agent: '.useragent_code(),
);

return $header;
}

视频地址解析方法

三、获取链接ID,我这里使用一个分割模块来进行操作。

/*
* 文本截取,第一个参数为需要截取的总个文本,第二个参数为需要截取的左边,第三个参数为需要截取的右边,返回被截取的文本
* */

function text_capture($txt, $left, $right)
{
#$ix = substr($txt, strlen($left)+strpos($txt, $left),(strlen($txt) - strpos($txt, $right))*(-1));
$b = substr($txt, 0, stripos($txt, $left, 0) + strlen($left));
$i = str_replace($b, "", $txt);
$ie = stripos($i, $right, 0);
if ($ie !== false) {
$ip = strlen($i) - $ie;
$d = substr($i, $ie, $ie + $ip);
$ix = str_replace($d, "", $i);
} else {
$ix = $i;
}
return $ix;
}

四、后台生成播放器代码

$id = "<video src=\"$ide\" type=\"video/mp4\" controls=\"controls\" width=\"640\" height=\"370\"></video><br>";

视频地址解析方法 视频地址解析方法 视频地址解析方法

原创文章,作者:夜风博客,如若转载,请注明出处:https://www.homedt.net/25582.html