Loading... ## 前言 > 之前做的人人商城互动直播修复插件近期有一些小伙伴联系我说插件失效了,看了下发现是抖音直播接口升级了,这里重写了一个php抖音直播源抓取。 ## 源码 抖音所有直播相关的信息其实都在源码当中写好的,都写在源码当中的json里,我们直接读取是可以读取到的,通过访问`https://v.douyin.com/*****` 这段网址,读取源码我们发现,所有的信息都在`window.__INIT_PROPS__`这个部分当中,而这部分内容是json结构的,所以我们只要将这部分代码转为能够读取的json就可以。 ## 编写 ```php <?php $url=$_GET['url']; $html=get_douyin($url); preg_match_all('/window.__INIT_PROPS__ =(.*)<\/script><\/head>/', $html, $jsmatch); if(!empty($jsmatch[1][0])){ $content=json_decode($jsmatch[1][0],1); if(!empty($content['/webcast/reflow/:id']['room']['stream_url']['hls_pull_url'])){ $data=array( 'code'=>200, 'success'=>true, 'data'=>array( 'url'=>$content['/webcast/reflow/:id']['room']['stream_url']['hls_pull_url'], 'title'=>$content['/webcast/reflow/:id']['room']['title'] ) ); echo json_encode($data); }else{ $data=array( 'code'=>400, 'success'=>false, 'msg'=>'数据接口请求失败' ); echo json_encode($data); } } function get_douyin($url,$post='',$cookie='', $returnCookie=0,$header=array(),$referer=''){ $curl = curl_init(); if(!empty($header)){ curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_HEADER, 0); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 8.1.0; en-US; Nexus 6P Build/OPM7.181205.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.11.1.1197 Mobile Safari/537.36'); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_AUTOREFERER, 1); curl_setopt($curl, CURLOPT_REFERER, $referer); if($post) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); } if($cookie) { curl_setopt($curl, CURLOPT_COOKIE, $cookie); } curl_setopt($curl, CURLOPT_HEADER, $returnCookie); curl_setopt($curl, CURLOPT_TIMEOUT, 10); 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; } } ``` 最后修改:2021 年 05 月 24 日 11 : 40 AM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 赞赏作者 支付宝微信