一个简单的PHP视频解析源码

2022年03月2日

原创内容,转载请注明出处:https://www.myzhenai.com.cn/post/4052.html

我们都知道,有很多网页上的视频通过读取网页源码获取到的视频直链地址是有时效的,所以就算把这个地址用到其他地方也是会失效的。

但是网页播放视频的原理肯定是需要一个地址的,有时候我们就可以通过PHP实时抓取它网页中的地址,然后放到自己的网页内容中。

实现原理非常简单,无非就是用PHP去抓取视频播放页面的网页源码,再分析出视频的地址就可以了。

<html>
<head>
<body>
<?php
/*
 * 正则获取视频的地址
 * */
function hinews_get_videourl($text, $md)
{
    $str = $text;
    $isMatched = preg_match("/https(.*?){$md}/", $str, $matches);
    $i = $matches[0];
    return $i;
}

/* hinews header*/
function hinews_header()
{
        $host = ""; // host
        $refere = ""; // Refere
    $header = array(
        "Host: {$host} ",
        "Referer: {$refere} ",
        "Content-Type: text/html",
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
        'Connection:keep-alive',
        "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:98.0) Gecko/20100101 Firefox/9",
    );
    return $header;
}

/**
 * 模拟浏览器开始访问请求
 * @param $url
 * @return bool|string
 */
function hinews_fetch_url($url) //这一段已经启用于视频
{
    $header = hinews_header();
    $timeout = 40;
    $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, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:98.0) Gecko/20100101 Firefox/9");
    $content = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    } else {
        return $content;
    }
    curl_close($ch);
}

function hinews_video($url)
{
    $t = hinews_fetch_url($url);
    if (stripos($t, ".mp4", 0) !== false) {
        $i = hinews_get_videourl($t, ".mp4");
        $id = "<video src=\"$i\" type=\"video/mp4\" controls=\"controls\" width=\"640\" height=\"370\"></video><br>";
    } elseif (stripos($t, ".ts", 0) !== false) {
        $i = hinews_get_videourl($t, ".ts");
        $id = "<video src=\"$i\" type=\"video/mp4\" controls=\"controls\" width=\"640\" height=\"370\"></video><br>";
    } else {
        $i = "no video";
    }
    return $id;
}
?>
<?php echo hinews_video("video_page_URL"); ?>
</body>
</head>
</html>

 

这里最主要的几点是,视频网页的 Host、Referer、UserAgent几个重要的参数


sicnature ---------------------------------------------------------------------
Your current IP address is: 54.226.68.181
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source http://myzhenai.com/post/4052.html

1 评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注