lizirong 发表于 2025-3-1 20:12:30

抖音php问题

本帖最后由 lizirong 于 2025-3-3 14:19 编辑

获取没反应,大佬看看哪里出错了
<?php
error_reporting(0);
$rid=$_GET['id'];
$url ="https://live.douyin.com/webcast/room/web/enter/?aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&enter_from=web_live&cookie_enabled=true&screen_width=1728&screen_height=1117&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Chrome&browser_version=116.0.0.0&web_rid=".$rid;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
    "Referer: https://live.douyin.com/"));

$data = curl_exec($ch);
curl_close($ch);
$json= json_decode($data,true);
$playurl= $json['data']['data']['stream_url']['flv_pull_url']['HD1'];
header('Location:'.$playurl);
?>

momo199412 发表于 2025-3-1 21:41:56

<?php

$rid = empty($_GET['id']) ? "37917621268" : trim($_GET['id']);
$stream = empty($_GET['media']) ? "flv" : trim($_GET['media']);

function mk_dir($newdir)
{
    $dir = $newdir;
    if (is_dir('./' . $dir)) {
      return $dir;
    } else {
      mkdir('./' . $dir, 0777, true);
      return $dir;
    }
}

function getDouYinUrl($rid, $stream,$cookietext)
{
    $liveurl = "https://live.douyin.com/" . $rid;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $liveurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookietext);
    $response = curl_exec($ch);
    preg_match('/__ac_nonce=(.*?);/', $response, $matches);
    $ac_nonce = $matches;

    curl_setopt($ch, CURLOPT_URL, $liveurl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: __ac_nonce=" . $ac_nonce));
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookietext);
    $response = curl_exec($ch);
    preg_match('/ttwid=.*?;/', $response, $matches);
    $ttwid = $matches;
    $url = "https://live.douyin.com/webcast/room/web/enter/?aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&enter_from=web_live&cookie_enabled=true&screen_width=1728&screen_height=1117&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Chrome&browser_version=116.0.0.0&web_rid=" . $rid;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
      "Cookie: " . $ttwid,
      "Accept: */*",
      "Host: live.douyin.com",
      "Connection: keep-alive"
    ));
    $response = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($response, true);
    $status = $json['data']['data']['status'];
    if ($status != 2) {
      return null;
    }
    $value = json_decode($json['data']['data']['stream_url']['live_core_sdk_data']['pull_data']['stream_data'],true);
    $realurl = "";
    foreach ($value as $key => $val) {
      if (array_key_exists('origin',$val)){
            if ($stream == "flv") {
                $realurl = $val['origin']['main']['flv'];
            } elseif ($stream == "hls") {
                $realurl = $val['origin']['main']['hls'];
            }
      }
    }
    return $realurl;

}

$cookietext = './' . mk_dir('cookies/') . md5(microtime()) . '.' . 'txt';
$mediaurl = getDouYinUrl($rid, $stream,$cookietext);
unlink($cookietext);
if ($mediaurl!=null){

    header('location:' . $mediaurl);
    exit();
}

ott 发表于 2025-3-5 18:50:45

<?php
$id = $_GET['id'];
$apiUrl = 'https://live.douyin.com/webcast/room/web/enter/?aid=6383&device_platform=web&browser_language=zh-CN&browser_platform=iPad&browser_name=Safari&browser_version=17.6&web_rid=' . $id;
$options = ['http' => ['method' => 'GET', 'header' => ['Cookie: ttwid=1%7CYqdH7G5MetcLz-SYn5w09SR81BSavm6lW81orliZUbA%7C1719753328%7Ccb83acfa23ce88b920c26fef1746820e6a701528adfe7c48847cfb5925eb1a0f']]];
$json = json_decode(file_get_contents($apiUrl, false, stream_context_create($options)), true);
$stream_data_json = $json['data']['data']['stream_url']['live_core_sdk_data']['pull_data']['stream_data'];
$stream_data = json_decode($stream_data_json, true);
//$hls_url = $stream_data['data']['origin']['main']['hls'];
$flv_url = $stream_data['data']['origin']['main']['flv'];
header('location:' . $flv_url);

lizirong 发表于 2025-3-7 09:08:06

momo199412 发表于 2025-3-1 21:41


谢了,这个我知道

lizirong 发表于 2025-3-7 09:10:14

ott 发表于 2025-3-5 18:50


感谢大佬帮助

jasonshu2025 发表于 2025-3-7 18:22:19

ott 发表于 2025-3-5 18:50


真棒,简洁有效

小蜜蜂 发表于 2025-3-7 20:25:35

大佬们,能给个id号测试一下吗?谢谢
页: [1]
查看完整版本: 抖音php问题