知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏壧峁┍憬莸闹С郑?
您當(dāng)前位置>首頁 » 新聞資訊 » 公眾號相關(guān) >
獲取微信公眾號文章內(nèi)視頻真實(shí)地址
發(fā)表時間:2020-10-28
發(fā)布人:葵宇科技
瀏覽次數(shù):83
PHP獲取微信公眾號文章內(nèi)視頻與音頻真實(shí)地址
微信公眾號的文章的爬取可以看到這個項(xiàng)目:github 基于QueryList的微信公眾號文章獲取工具
有任何問題可以留言。
返回數(shù)據(jù):
- 包含音頻和視頻
- 包含多個視頻
原有找到的文章 如何抓取微信公眾號文章里面的視頻 中間只有原本的公眾號文章內(nèi)的騰訊視頻的獲取,本方案增加了微信視頻素材的獲取和音頻的獲取。優(yōu)化了騰訊視頻獲取的方案。
- 文章內(nèi)騰訊視頻的真實(shí)地址獲取
- 文章內(nèi)的視頻素材的真實(shí)地址獲取
- 文章內(nèi)的音頻的獲取
- 文章資源下載
請求
<?php
//var_dump($_GET);
$con = new Con();
$url = $_GET['url'];
$res = $con->actionGetwx($url);
var_dump($res);
die;
獲取資源
/**
* Class con
* @author Fawkes
* @time 2020-10-28
*/
class con {
/**
* 抓取微信公眾號的文章和里面的視頻 url
* @param $url
* @return array|bool
*/
public function actionGetwx($url) {
if (empty($url)) {
echo '請輸入公眾號文章地址';
return false;
}
$info_id_arr = $this->getChatInfoId($url);
//獲取真實(shí)地址鏈接
$info_arr = [];
foreach ($info_id_arr as $key=>$value){
//獲取視頻
switch ($key){
case 'video':
$info_arr['video'] = [];
if(!empty($value)){
foreach ($value as $vid){
$video_json = Tools::getVqqInfo($vid);
if(!empty($video_json['msg']) && $video_json['msg'] == 'vid is wrong'){
//檢測微視
$return = $this->weishiQQCom($vid);
}else{
//騰訊視頻的真是地址獲取
$return = $this->vQQCom($video_json);
}
$info_arr['video'][] = $return;
};
}
break;
case 'voice':
$info_arr['voice'] = [];
if(!empty($value)){
foreach ($value as $vid){
$return = $this->voiceInfo($vid);
$info_arr['voice'][] = $return;
};
}
break;
default:
break;
}
}
return $info_arr;
}
/**
* 獲取公眾號中的資源 音頻和視頻
* @param $url
* @return array
*/
public function getChatInfoId($url){
//微信的鏈接有長鏈和短鏈,以下為長鏈
//$url ='http://mp.weixin.qq.com/s?__biz=MzI0NTc1MTczNA==&mid=2247485130&idx=1&sn=945cfb8b0cfdd99f1b730889de0216e2&chksm=e9488c13de3f05057be6c6b065f8e44d43c566cb9ee3a4f35cf8084382742159181ea480b935&scene=27';
if (stripos($url, '?')) {
if (stripos($url, '#wechat_redirect')) {
$url = str_replace('#wechat_redirect', '', $url);
}
$json = $url . '&f=json';
} else {
$json = $url . '?f=json';
}
$data = Tools::curl_request($json);
$data = json_decode($data, 1);
$chat_info_id = [];
//獲取json中的得到視頻vid
$vid_arr = $data['video_ids'] ?? [];
//獲取json中的得到音頻的mid
$voice_arr = array_column($data['voice_in_appmsg'],'voice_id') ?? [];
if(empty($vid_arr)){
//data 為文章的詳情
$html = $data['content_noencode'];
preg_match_all('/<iframe (.*?)data-src="(.*?)">/', $html, $matchs);
//沒有視頻腳本退出
if (empty($matchs[2])) {
echo '沒有視頻匹配到,不采集';
die;
}
//判斷是否是url地址 而后解析得出 vid的值
$url = current($matchs[2]);
if(!filter_var($url,FILTER_VALIDATE_URL)){
echo '視頻地址異常:'.$url;
die;
}
$url = str_replace('&','&',$url);
//https://v.qq.com/iframe/preview.html?vid=i1324786hv8&width=500&height=375&auto=0
$url_arr = parse_url($url);
$query = $url_arr['query'] ?? '';
$vidArray = explode("&vid=",$query);
//獲取到vid
$vid_arr = [$vidArray[1]] ?? '';
if(empty($vid_arr)){
echo '視頻參數(shù)異常:'.$query;
die;
}
}
$chat_info_id['video'] = $vid_arr;
$chat_info_id['voice'] = $voice_arr;
return $chat_info_id;
}
/**
* 騰訊微視獲取真實(shí)地址
*/
private function weishiQQCom($vid){
$url = 'https://mp.weixin.qq.com/mp/videoplayer?action=get_mp_video_play_url&vid='.$vid;
$data = Tools::curl_request($url);
$data = json_decode($data,1);
//得到數(shù)據(jù)的json 組裝成功url
$format_id = $data['url_info'][0]['format_id'];
$title = $data['title'];
$url = $data['url_info'][0]['url']."&vid=$vid&format_id=$format_id";
return [
'vid' => $vid,
'type' => '公眾號素材視頻',
'title' => $title,
'url' => $url
];
}
/**
* 騰訊視頻的處理url
* @param array $video_json 騰訊視頻數(shù)據(jù)
* @return array
*/
private function vQQCom(array $video_json){
$title = $video_json['vl']['vi'][0]['ti'];
$vid = $video_json['vl']['vi'][0]['vid'];
//高質(zhì)量視頻
$fn_pre = $video_json['vl']['vi'][0]['lnk'];
$host = $video_json['vl']['vi'][0]['ul']['ui'][0]['url'];
$streams = $video_json['fl']['fi'];
$seg_cnt = $video_json['vl']['vi'][0]['cl']['fc'];
$best_quality = end($streams)['name'];
$part_format_id = end($streams)['id'];
$part_urls = [];
for ($part = 1; $part <= $seg_cnt + 1; $part++) {
$filename = $fn_pre . '.p' . ($part_format_id % 10000) . '.' . $part . '.mp4';
$key_api = "http://vv.video.qq.com/getkey?otype=json&platform=11&format="
. $part_format_id . "&vid=" . $vid . "&filename=" . $filename . "&appver=3.2.19.333";
$part_info = Tools::curl($key_api);
preg_match('/QZOutputJson=(.*);$/Uis', $part_info, $key_json);
$key_json = json_decode($key_json[1], 1);
if (empty($key_json['key'])) {
$vkey = $video_json['vl']['vi'][0]['fvkey'];
$url = $video_json['vl']['vi'][0]['ul']['ui'][0]['url'] . $fn_pre . '.mp4?vkey=' . $vkey;
} else {
$vkey = $key_json['key'];
$url = $host . $filename . "?vkey=" . $vkey;
}
$part_urls[] = $url;
}
//真實(shí)的地址
if (empty($part_urls)) {
//獲取的視頻質(zhì)量低
if (!empty($video_json['vl']['vi'])) {
$keys = [];
foreach ($video_json['vl']['vi'] as $key => $value) {
$fvkey = $value['fvkey'];
$fn = $value['fn'];
$self_host = $value['ul']['ui'][$key]['url'];
$keys['fvkey'] = $fvkey;
$keys['fn'] = $fn;
$keys['self_host'] = $self_host;
$keys['lnk'] = $value['lnk'];
}
$part_urls[0] = $keys['self_host'] . $keys['fn'] . '?vkey=' . $keys['fvkey'];
}
}
return [
'vid' => $vid,
'type' => '騰訊視頻',
'title' => $title,
'url' => current($part_urls)
];
}
/**
* 獲取音頻真實(shí)地址
* @param string $vid
*/
private function voiceInfo(string $vid){
$url = 'https://res.wx.qq.com/voice/getvoice?mediaid='.$vid;
return [
'vid' => $vid,
'type' => '音頻資料',
'url' => $url
];
}
}
使用的工具類
class Tools {
public static function httpcopy($url, $file="", $timeout=60) {
$file = empty($file) ? pathinfo($url,PATHINFO_BASENAME) : $file;
$dir = pathinfo($file,PATHINFO_DIRNAME);
!is_dir($dir) && @mkdir($dir,0755,true);
$url = str_replace(" ","%20",$url);
if(function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$temp = curl_exec($ch);
if(@file_put_contents($file, $temp) && !curl_error($ch)) {
return $file;
} else {
return false;
}
} else {
$opts = array(
"http"=>array(
"method"=>"GET",
"header"=>"",
"timeout"=>$timeout)
);
$context = stream_context_create($opts);
if(@copy($url, $file, $context)) {
return $file;
} else {
return false;
}
}
}
public static function curl_request($url, $post = '',$header = [], $cookie = '', $returnCookie = 0) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
if ($post) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
}
if($header){
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
}
if ($cookie) {
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
}
curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
if ($returnCookie) {
list($header, $body) = explode("\r\n\r\n", $data, 2);
preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
$info['cookie'] = substr($matches[1][0], 1);
$info['content'] = $body;
return $info;
} else {
return $data;
}
}
public static function getVqqInfo($vid) {
// $json_type = 'json';
// $platform = '11001';
// $guid = 'ba7f7fab8f8aef2c4ae45883f221c04d';
// $sdtfrom = 'v1010';
// $_qv_rmt = '703hXQMOA1937593I%3D';
// $_qv_rmt2 = 'egp3C2aj149060UUA%3D';
// $url = sprintf('http://vv.video.qq.com/getinfo?vid=%s&otype=%s&guid=%s&platform=%s&sdtfrom=%s&_qv_rmt=%s&_qv_rmt2=%s', $vid, $json_type, $guid, $platform, $sdtfrom, $_qv_rmt, $_qv_rmt2);
$infourl = 'https://vv.video.qq.com/getinfo?otype=json&appver=3.2.19.333&platform=11&defnpayver=1&vid=' . $vid;
$data = self::curl($infourl);
preg_match('/QZOutputJson=(.*);$/Uis', $data, $info);
return json_decode($info[1], 1);
}
public static function getExt($url) {
$urlinfo = parse_url($url);
$file = basename($urlinfo['path']);
if (strpos($file, '.') !== false) {
$ext = explode('.', $file);
return $ext[count($ext) - 1];
}
return 'no extension';
}
public static function strFilter($str) {
$str = str_replace(' ', '', $str);
$str = str_replace(' ', '', $str);
$str = str_replace('`', '', $str);
$str = str_replace('·', '', $str);
$str = str_replace('~', '', $str);
$str = str_replace('!', '', $str);
$str = str_replace('!', '', $str);
$str = str_replace('@', '', $str);
$str = str_replace('#', '', $str);
$str = str_replace('$', '', $str);
$str = str_replace('¥', '', $str);
$str = str_replace('%', '', $str);
$str = str_replace('^', '', $str);
$str = str_replace('……', '', $str);
$str = str_replace('&', '', $str);
$str = str_replace('*', '', $str);
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
$str = str_replace('-', '', $str);
$str = str_replace('_', '', $str);
$str = str_replace('——', '', $str);
$str = str_replace('+', '', $str);
$str = str_replace('=', '', $str);
$str = str_replace('|', '', $str);
$str = str_replace('\\', '', $str);
$str = str_replace('[', '', $str);
$str = str_replace(']', '', $str);
$str = str_replace('【', '', $str);
$str = str_replace('】', '', $str);
$str = str_replace('{', '', $str);
$str = str_replace('}', '', $str);
$str = str_replace(';', '', $str);
$str = str_replace(';', '', $str);
$str = str_replace(':', '', $str);
$str = str_replace(':', '', $str);
$str = str_replace('\'', '', $str);
$str = str_replace('"', '', $str);
$str = str_replace('“', '', $str);
$str = str_replace('”', '', $str);
$str = str_replace(',', '', $str);
$str = str_replace(',', '', $str);
$str = str_replace('<', '', $str);
$str = str_replace('>', '', $str);
$str = str_replace('《', '', $str);
$str = str_replace('》', '', $str);
$str = str_replace('.', '', $str);
$str = str_replace('。', '', $str);
$str = str_replace('/', '', $str);
$str = str_replace('、', '', $str);
$str = str_replace('?', '', $str);
$str = str_replace('?', '', $str);
return trim($str);
}
public static function curl($url, $option = []) {
$split = explode('/', $url);
$cookiejar = str_replace('\\', '/', dirname(__FILE__)) . '/' . $split[2] . '.cookie';
$ch = curl_init();
$options = $option?$option:[
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => [
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Charset: UTF-8,*;q=0.5",
"Accept-Encoding': 'gzip,deflate,sdch",
"Accept-Language': 'en-US,en;q=0.8",
"User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0",
],
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_COOKIEJAR => $cookiejar,
CURLOPT_COOKIEFILE => $cookiejar,
];
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}
相關(guān)案例查看更多
相關(guān)閱讀
- 做網(wǎng)站
- vue開發(fā)小程序
- 小程序表單
- 網(wǎng)站建設(shè)選
- 云南網(wǎng)站建設(shè)服務(wù)
- 汽車報廢回收軟件
- 云南網(wǎng)頁制作
- 汽車報廢回收管理系統(tǒng)
- 云南網(wǎng)站優(yōu)化公司
- APP
- 網(wǎng)站建設(shè)首頁
- 云南網(wǎng)站建設(shè)公司排名
- 網(wǎng)絡(luò)公司哪家好
- 云南網(wǎng)絡(luò)營銷顧問
- 網(wǎng)絡(luò)公司
- 昆明小程序開發(fā)聯(lián)系方式
- 云南網(wǎng)站建設(shè)哪家好
- 怎么做網(wǎng)站
- 小程序被騙退款成功
- 小程序制作
- 云南小程序被騙蔣軍
- 汽車回收管理
- 云南網(wǎng)站建設(shè)特性
- 云南軟件設(shè)計
- 云南小程序公司
- 小程序設(shè)計
- 云南小程序開發(fā)課程
- 云南小程序開發(fā)制作
- 網(wǎng)絡(luò)公司電話
- 云南軟件定制公司