zl程序教程

您现在的位置是:首页 >  Python

当前栏目

抖音背景音乐获取API

2023-04-18 15:57:17 时间

有时候看到好看的抖音,听到喜欢听的背景音乐但音乐却没有在音乐软件上架怎么办?下面代码直接实现。

实现原理

随便用一个妹妹试一下,咱主要不是看妹子,是单纯的想要背景音乐而已好吧。 从抖音复制来的链接 https://v.douyin.com/JwR69Lk

直接用做成的api解析一下,默认是这个链接,可以做成api,直接后面跟 url参数 就行了,直接就得到了背景音乐。例如: 我做个API测试了一下:https://www.q6q.cc/api/douyinbgm/api.php?url=https://v.douyin.com/JwR69Lk ,直接访问就能看到背景音乐直链。

         const ap399 = new APlayer({             container: document.getElementById("aplayer399"),             audio: [{                 name: "测试",                 artist: "强仔测试",                 url: "https://sf3-cdn-tos.douyinstatic.com/obj/ies-music/6887372313550916366.mp3",                 cover: "https://p11.douyinpic.com/img/tos-cn-avt-0015/5c8be307847478934f00d53c3a62ddf4~c5_1080x1080.jpeg?from=2563711402"             }]         });     

代码代码

<?php
$url = empty($_GET['url'])?"https://v.douyin.com/JwR69Lk":$_GET['url'];
header('Content-type: application/json; charset=utf-8');
$body = get_curl($url,0,"https://v.douyin.com",0,1,0,1);
preg_match("/Location: (.*?)
/iU", $body, $urls);
if(!$urls[1]){
exit("error");
}
$dyurl = $urls[1];
preg_match("/video/(.*?)//s",$dyurl,$item_ids);
if(!$item_ids[1]){
exit(json_encode(['code'=>-1,'msg'=>'解析链接失败'],320));
}
$item_ids = $item_ids[1];
$api = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=".$item_ids;
$boby = get_curl($api);
$json = json_decode($boby);
$item_list = $json->item_list[0];
if(!$item_list){
exit(json_encode(['code'=>-1,'msg'=>'获取详细信息失败'],320));
}
$author = $item_list->author;
$nickname = $author->nickname;//获取抖音昵称
$unique_id = $author->unique_id;//获取抖音号
$author_tx = $author->avatar_larger->url_list[0];//获取作者高清头像
//获取视频介绍
$desc = $item_list->desc;
//获取视频背景音乐
$music = $item_list->music;
$music_url = $music->play_url->uri;

$return=[
'code'=>1,
'nickname'=>$nickname,
'unique_id'=>$unique_id,
'desc'=>$desc,
'author_tx'=>$author_tx,
'music_url'=>$music_url,
];
exit(json_encode($return,320));
function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0,$split=0){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpheader[] = "Accept:*/*";
$httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
$httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
$httpheader[] = "Connection:close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
if($post){
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if($header){
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
}
if($cookie){
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if($referer){
    curl_setopt($ch, CURLOPT_REFERER, $referer);
}
if($ua){
    curl_setopt($ch, CURLOPT_USERAGENT,$ua);
}else{
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36');
}
if($nobaody){
    curl_setopt($ch, CURLOPT_NOBODY,1);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
$ret = curl_exec($ch);
if ($split) {
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($ret, 0, $headerSize);
    $body = substr($ret, $headerSize);
    $ret=array();
    $ret['header']=$header;
    $ret['body']=$body;
}
curl_close($ch);
return $ret;
}
?>