PHP获取抖音热门视频
public function hotVideo()
{
$url = "https://creator.douyin.com/web/api/creator/material/center/billboard/";
$cookies = 'gfkadpd=2906,33638';
$jsonRes = json_decode(get($url, [], $cookies), true);
if (!isset($jsonRes['item_list']) && empty($jsonRes['item_list'])) {
$data = Cache::get('hot_video');
} else {
Cache::set('hot_video', $jsonRes);
$data = Cache::get('hot_video');
}
return $this->success('成功', $data);
}
function get($url, $params = [], $cookies = '', $timeout = 30)
{
if (!empty($params)) {
$url .= '?' . http_build_query($params);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($cookies)) {
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
}
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
return false;
}
curl_close($ch);
return $response;
}
public function getDyHot(Request $request)
{
$res = $this->hotVideo();
$res = $res->getData();
$List = [];
if (isset($res['data']['item_list'])) {
foreach ($res['data']['item_list'] as $val) {
$lists = [];
$lists['author_link'] = $val['author_link'];
$lists['author_name'] = $val['author_name'];
$lists['comment_count'] = round($val['comment_count'] / 10000, 1) . '万';
$lists['like_count'] = round($val['like_count'] / 10000, 1) . '万';
$lists['play_count'] = round($val['play_count'] / 10000, 1) . '万';
$lists['title'] = $val['title'];
$lists['img'] = $val['cover']['url_list'][0];
$lists['url'] = 'https://www.iesdouyin.com/share/video/' . $val['item_id'];
$List[] = $lists;
}
}
return success('查询成功', $List);
}