当前位置:首页 > 懒得分类 > 正文内容

php抖音解析API

<?php
/*
 * @Author       :    @祥💥 技术支持
 * @Mail         : 520@shejiz.cn
 * @Date         : 2024-08-15 17:04:09
 * @LastEditTime : 2024-08-15 17:36:07
 * @LastEditors  :    @祥💥 技术支持
 * @Description  : xxx.php?url=https://v.douyin.com/6W4jytR/
 * @FilePath     : *
 * It's up to you ^_^
 * Copyright (c) 2024 by 520@shejiz.cn, All Rights Reserved. 
 */

header('Content-Type: application/json; charset=utf-8');
$url = explode(' ',$_GET['url']);
$url = $url['0'];
if(strpos($url,'v.douyin.com') == false){
    echo json_encode(['code'=>201,'message'=>'非抖音链接'],256);exit();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: */*',
    'Accept-Encoding: gzip',
    'Connection: Keep-Alive',
    'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
preg_match("/Location: (.*?)\r\n/iU",$ret,$location);
if(!isset($location[1]))
{
    echo json_encode(['code'=>202,'message'=>'请重试'],256);exit();
}
$str = dirname($location[1]);
$itemId = substr($str,strripos($str,'video')+6);
$ch = curl_init('https://www.iesdouyin.com/share/video/' . $itemId);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/122.0.0.0'));
if (isset($data)) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
$res = curl_exec($ch);
$pattern = '/window\._ROUTER_DATA\s*=\s*(.*?)\<\/script>/s';
preg_match($pattern, $res, $matches);
if (!empty($matches[1])) {
    $videoInfo = json_decode(trim($matches[1]), true);
    if (isset($videoInfo['loaderData'])) {
        $result = array(
            'aweme_id' => $itemId,
            'author' => $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]['author']['nickname'],
            'uid' => $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]['author']['unique_id'],
            'avatar' => $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]['author']['avatar_medium']['url_list'][0],
            'like' => $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]['statistics']['digg_count'],
            'time' => $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]["create_time"],
            'title' => $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]['desc'],
            'cover' => $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]['video']['cover']['url_list'][0],
            'url' => str_replace('playwm', 'play', $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]['video']['play_addr']['url_list'][0]),
            'uri' => $videoInfo['loaderData']['video_(id)/page']['videoInfoRes']['item_list'][0]['video']['play_addr']['uri'],
        );
        echo json_encode($result,256);exit();
    }
}
echo json_encode(['code' => 201, 'msg' => '解析失败'],256);
?>


简单说两句

访客

◎ 不想说话可以不说,说了便要负责!