|
收集并整理了通过php的操作获取网页源码或接口数据的不同方法:
1. 通过file()函数
源码实例:迅雷美女直播;测试:http://mmitv.top/test/xunlei.php
<?php
$lines = file('https://live.xunlei.com');//file() 函数把整个文件读入一个数组中。
foreach ($lines as $line_num => $line) {
preg_match_all('/"stream_flv_pull_https":"(.*?)"/',$line,$m3u8);
if($m3u8[0]){break;}
}
$playurl=str_replace('\u002F','/',$m3u8[1][mt_rand(0,count($m3u8[1])-1)]);
//print_r($playurl);
header('location:'. $playurl);
?>
2. 通过file_get_contents()+stream_context_create函数
源码实例:成都电视1台;测试:http://mmitv.top/other/cdtv.php?id=cdtv1 http://mmitv.top/other/cdtv.php?ids=1
<?php
$url='https://www.cditv.cn/api.php?op=live&catid=2762&id=1610&videotype=m3u8&fluency=sd&type=playTv&startTime=&password=';
$headers=stream_context_create(['http'=>['header'=>['Referer: https://www.cditv.cn/']]]);//创建请求方式和头信息
$playurl=file_get_contents($url,false,$headers);
//print_r($playurl);
header('location:'. $playurl);
?>
3. 通过fopen()和fread()函数
源码实例:CC美女直播;测试:http://mmitv.top/test/cc.php?id=top
<?php
$handle = fopen('https://api.cc.163.com/v1/wapcc/liveinfo?gametype=65005&page='.mt_rand(1,5), 'r'); // 以只读方式打开文件并将指针指向文件头,资源类型
$contents = '';
while (!feof($handle)){ // 判断文件指针是否到了文件的末尾
$contents .= fread($handle, 1024); // 每次读取1024个字节的数据
}
fclose($handle); // 关闭文件
$json = json_decode($contents);
$id=$json->data->live_list[mt_rand(0,11)]->ccid;
$playurl = 'http://cgi.v.cc.163.com/redirect/video/'.$id.'.flv';
//echo $playurl;
header('location:'. $playurl);
?>
4. 通过php的curl扩展库;常用。
源码实例:KK美女直播;实例二测试:http://mmitv.top/test/kk.php
实例一:
<?php
$ch = curl_init();// 创建一个curl会话资源
curl_setopt($ch, CURLOPT_URL, "https://www.kktv5.com");// 设置curl相应的选项
curl_setopt($ch, CURLOPT_HEADER, 0);//不输出请求信息。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//结果返回,不自动输出任何内容。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);// 执行curl
curl_close($ch);// 关闭curl会话
preg_match_all('/target="actor_(\d.*?)".*?alt="(.*?)"/',$data,$str1);
preg_match_all('/a href="\/(\d.*?)" class="actor video_cover" target="_blank" title="(.*?)"/',$data,$str2);
$id=$str1[1]+$str2[1];
$playurl='https://pull.kktv8.com/livekktv/'.$id[mt_rand(0,count($id)-1)].'.flv';
//print_r($playurl);
header('location:'. $playurl);
?>
备注:实例一在我的服务器上curl不出数据。不知是网络限制、地域限制、还是服务器问题。还请坛友测试解惑...
实例二:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sapi.kktv1.com/meShow/entrance?parameter={"c":"8001","cid":"810","pageIndex":1,"countPerPage":10,"FuncTag":51070104,"userId":0,"platform":1,"a":1}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
curl_close($ch);
preg_match_all('/"liveStream":"(.*?)"/',$data,$str);
$playurl=$str[1][mt_rand(0,count($str[1])-1)];
//print_r($playurl);
header('location:'. $playurl);
?>
|
|