教程
抖音背景音乐获取API
343

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

实现原理

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

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

来首DJ,来首DJ~~~

代码代码

<?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: (.*?)\r\n/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;
}
?>

结语

感谢访问强仔博客,希望本文对你有所帮助!

  • 上一篇
  • 下一篇
  • 添加评论
    评论(2)
    夏目贵志
    夏目贵志

    你好骚啊

    welcome to qiangzai blog