(自写程序)随机生成tiktok热门视频API源码

 2023-10-19  阅读 161  评论 0  点赞 0

摘要:自己写着玩的程序,分享一下,要求:需要上传到最少是香港服务器或者你的服务器有科学上网的功能才可以

自己写着玩的程序,分享一下:

要求:需要上传到最少是香港服务器或者你的服务器有科学上网的功能或者有http代理才可以

<?php
/*================================================
作者信息: 老宋博客 [QQ:327954269] [www.weihangapi.cn]
=================================================*/
header('Content-type: application/json');
function g_sg5275($url, $proxy) {
    $user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36';
    $host = array("Host: www.tikwm.com");
    $cookieFile = tempnam(sys_get_temp_dir(), 'cookie'); // Cookie 文件

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中的名称是否与提供的主机名匹配
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $host);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); // 启用 Cookie 处理
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); // 启用 Cookie 处理

    // 使用代理
    curl_setopt($ch, CURLOPT_PROXY, $proxy);

    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
    }
    curl_close($ch);
    return $data;
}

function mp($url, $proxy) {
    $user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36';
    $cookieFile = tempnam(sys_get_temp_dir(), 'cookie'); // Cookie 文件

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中的名称是否与提供的主机名匹配
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); // 启用 Cookie 处理
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); // 启用 Cookie 处理

    // 使用代理
    curl_setopt($ch, CURLOPT_PROXY, $proxy);

    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
    }
    curl_close($ch);
    return $data;
}

error_reporting(0);
$proxy = 'http://xxx.xxx.xxx.xxx:38080'; // 替换为你的代理服务器
$a = 'https://www.tikwm.com/zh-Hans/';
$str = g_sg5275($a, $proxy);

if (!$str) {
    $arr = array(
        'code' => 201,
        'msg' => '无法获取网页内容'
    );
    echo json_encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

$pattern = '/<div\sclass="other-video-div\scol-lg-2\scol-md-3\scol-sm-4\scol-xs-6"[^>]*>\s*<a\shref="\/video([^"]*)"[^>]*>/';
preg_match($pattern, $str, $matches);

if (empty($matches)) {
    $arr = array(
        'code' => 201,
        'msg' => '未找到匹配的视频链接'
    );
    echo json_encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

$url = $matches[1];
$api = 'https://www.tikwm.com/api/?url=https://m.tiktok.com/v' . $url;
$op = mp($api, $proxy);
$json = json_decode($op, true);

if ($json && isset($json['msg']) && $json['msg'] == "success") {
    $arr = [
        'code' => 200,
        'msg' => '解析成功',
        'data' => [
            'title' => $json['data']['title'],
            'cover' => $json['data']['origin_cover'],
            'url' => $json['data']['play'],
        ]
    ];
} else {
    $arr = array(
        'code' => 201,
        'msg' => '不支持您输入的链接或解析失败'
    );
}

echo json_encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
?>

正常获取的代码为:

{
    "code": 200,
    "msg": "解析成功",
    "data": {
        "title": "what shall we call this line #fypage #skz #straykids #skzedit #straykidsedit #straykidsstay #edits #bangchan #channie #changbin #felix #bangchanedit #changbinedit #felixedit #blowthisup #fyppppppppppppppppppppppp #viral #trending #aftereffects #dontletthisflop #dontflop ",
        "cover": "https://p16-sign.tiktokcdn-us.com/tos-useast5-p-0068-tx/1ed2e1c253e44ae482edd166173c5557_1722370820~tplv-tiktokx-360p.jpeg?lk3s=d05b14bd&nonce=51786&refresh_token=92754715ef88aa46bbaf8a5a794c587c&x-expires=1722906000&x-signature=ijGi4UgMRzJfI7y0un3IJL9GALA%3D&s=AWEME_DETAIL&se=false&sh=&sc=feed_cover&l=20240805011841B0E20206713A6B1A83F6&shp=d05b14bd&shcp=-",
        "url": "https://v16m-default.akamaized.net/cf6314c988ec869f5bd3d95675d27531/66b07ce2/video/tos/maliva/tos-maliva-ve-0068c799-us/okuUQkI7XGAhlfHL9pLBHjNgtI6AICeUIVeySI/?a=0&bti=OUBzOTg7QGo6OjZAL3AjLTAzYCMxNDNg&ch=0&cr=0&dr=0&lr=all&cd=0%7C0%7C0%7C0&cv=1&br=3978&bt=1989&cs=0&ds=6&ft=XE5bCqT0majPD12kEv273wUOx5EcMeF~O5&mime_type=video_mp4&qs=0&rc=Zjg2NDQ7aDs7NzRnM2hoOEBpMzdlaWo5cjVudDMzZzczNEA0NDM0Xi02XzAxXy5iYWJiYSMvbnFjMmRzaWhgLS1kMS9zcw%3D%3D&vvpl=1&l=20240805011841B0E20206713A6B1A83F6&btag=e00088000&shp=6da16bae&shcp=-"
    }
}
标签:自写程序php

发表评论:

管理员

承接各种程序开发,外贸网站代运营,外贸网站建设等项目
  • 内容185
  • 积分66666
  • 金币66666

Copyright © 2024 LS'Blog-保定PHP程序员老宋个人博客 Inc. 保留所有权利。 Powered by LS'blog 3.0.3

页面耗时0.0247秒, 内存占用1.89 MB, 访问数据库23次

冀ICP备19034377号