微博上有很多自媒体视频,种类繁多。有时候看到好的视频想要保存,通过浏览器开发者工具查看微博视频地址总是比较麻烦。

为了方便,银狐写了一个微博视频地址解析php代码。只需要将以下微博视频地址解析php代码复制到新建的php文件中,将文件上传到主机,通过浏览器访问api地址即可解析出视频地址,鼠标右键即可下载。

php解析微博视频地址api代码

<?php
//使用方法:weibo.php?url=微博视频地址
error_reporting(0);
$domain = trim($_SERVER['SERVER_NAME']);
$opendomain = array("127.0.0.1","localhost","ityinhu.com");
if(in_array($domain, $opendomain)){
$url = isset($_GET['url']) ? addslashes($_GET['url']) : 'https://weibo.com/tv/show/1034:4611811694018581';
$url = str_replace('?from=old_pc_videoshow','',$url);
preg_match('/show\/(.*)/i', $url, $matches);
$fid = $matches[1];
if(!$fid){die('银狐提醒:获取错误,请检测微博视频网址是否正确。');}
$content = 'https://video.h5.weibo.cn/s/video/object?object_id='.$fid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$content);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_REFERER,$url);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36');
$output = curl_exec($ch);curl_close($ch);$data = json_decode($output,true);
if($data['ok']){
$sd_url = str_replace('http','https',$data['data']['object']['stream']['url']);
$hd_url = str_replace('http','https',$data['data']['object']['stream']['hd_url']);
if($hd_url){
header('location:'.$hd_url);exit();
}else{
header('location:'.$sd_url);exit();
}
}else{
die('银狐提醒:获取错误,请检测微博视频网址是否正确。');
}
}else{
die('银狐提醒:请勿盗链。');
}

演示地址:https://www.ityinhu.com/api/wb/video/weibo.php

2021年5月13日更新,该代码已经失效