robert-grubb / tiktok-php
PHP TikTok 爬虫
Requires
- php: >=5.4.0
- mouf/nodejs-installer: ^1.0
Requires (Dev)
- phpunit/phpunit: 5.5.*
This package is auto-updated.
Last update: 2024-09-29 15:27:42 UTC
README
By default, this scraper will attempt to use NodeJS to sign the URL. If you do not have node installed, it will attempt to install it during the composer install step. If you do, it will simply find the path to it. If you'd like to change this logic, you can read more below about setting your signMethod => 'datafetch', an API I have created for signing tiktok urls.
1.9.14
对 user->account
方法进行了更新。响应对象没有变化。
1.9.13
引入了 $scraper->user->videoFromEmbed('id');
这将返回嵌入端点的视频数据。目前它比正常视频路由更稳定。唯一的缺点是它不返回
1.9.12
现在可以手动设置 verifyFp,因为验证码验证已经风靡 TikTok。你可以这样设置
$scraper = new Scraper([
'verifyFp' => 'verify_xxxxx_xxxx_xxxx_xxx...'
])
如果你没有设置,默认情况下,爬虫将尝试为你生成以下 cookies
verifyFp, tt_webid, and tt_webid_v2
然而,在当前大多数情况下,fp token 将成为触发验证码的原因。
Cookie 文件
在 v1.8.0
版本中,爬虫现在默认设置 cookies,这意味着你必须为 cookies.json
文件提供可写权限,然后在配置中设置它(请参阅下面的示例配置)。
需要禁用 cookies?在配置中设置 'disableCookies' => true
。
安装
composer require robert-grubb/tiktok-php
实例化
require './vendor/autoload.php';
use TikTok\Scraper;
// Instantiate TikTok Scraper library
$scraper = new Scraper([
// Sign method
'signMethod' => 'datafetch'
'datafetchApiKey' => ''
'userAgent' => '',
'proxy' => [
'protocol' => 'http',
'address' => '127.0.0.1:8080',
'auth' => 'username:password'
],
'timeout' => 20,
// Since v1.8.0 (Must set cookie file)
'cookieFile' => __DIR__ . '/cookies.json'
// If not using cookies:
'disableCookies' => true
]);
配置说明
[
// Sign method
'signMethod' => 'node' // Or datafetch
// User agent
'userAgent' => '',
// Optional proxy (auth is also optional)
'proxy' => [
'protocol' => 'http',
'address' => '127.0.0.1:8080',
'auth' => 'username:password'
],
// Time before curl request times out
'timeout' => 20
]
搜索用户
// 30 being the number of results
$scraper->user->search('username', 30);
用户数据
$scraper->user->details('username');
用户视频
$scraper->user->videos(123415125125);
// Or use username, 10 is the count of videos to return.
$scraper->user->videos('iratee', 10);
所有用户视频
$scraper->user->allVideos(123415125125);
// Or use username
$scraper->user->allVideos('iratee');
特定用户视频
$scraper->user->video('username', 123415125125);
下载用户视频
/**
* 1st Param: username
* 2nd Param: video_id
* 3rd Param: watermark (default true)
* 4th Param: path to download
*/
$scraper->user->downloadVideo('username', 123415125125, true, './');
发现
$scraper->discover->get('music');
$scraper->discover->get('user');
$scraper->discover->get('hashtag');
// Offset and count:
$scraper->general->discover('music', [
'count' => 10,
'offset' => 10
]);
热门
// 25 being the number of items to return.
$scraper->trending->videos(25);
音乐数据
// Gets data for music by id
$scraper->music->data(12312512512);
音乐视频
// Gets videos for music (25 being the count)
$scraper->music->videos(12124124124, 25)
按 ID 下载音乐
/**
* 1st Param: music_id
* 2nd Param: path (optional)
* 3rd Param: custom name (optional)
*/
$scraper->music->download(6821468236035541766, './', 'custom-name');
哈希标签数据
$scraper->hashtag->data('beatbox');
哈希标签视频
// 30 being the count of videos to return
$scraper->hashtag->videos('beatbox', 30);
签名 URL
提供了两种签名 URL 的方法。
-
你可以使用 nodejs,这个包会检查你是否已安装它,如果没有,它会尝试安装它。
-
你可以使用我亲自构建的 datafet.ch api 来接受来自任何人的请求以签名 TikTok URL。(如果你在配置中设置
'signMethod' => 'datafetch'
,这个包会自动处理。)
$scraper->signUrl('TIKTOK_URL_HERE');
DataFetch API 密钥
DataFetch API 对你的请求进行速率限制,最多每 15 分钟 100 个请求。你可以通过获取 API 密钥来避免这个问题,这会给你提供无限的请求权限。你可以通过联系 matt [at] grubb [dot] com
来获取访问权限。
错误
如果在爬虫过程中发生任何错误,它将在以下位置设置错误
$scraper->error
它将返回以下结构
[
'error' => true,
'message' => 'Detailed error message here.'
]
此外,你调用的方法也将返回 false
。
如果没有错误,你会注意到两件事
$scraper->error
设置为false
- 你调用的方法不会返回
false
法律
这个存储库及其内容与 TikTok 或其任何附属公司或子公司无关,未获授权、维护、赞助或认可。这是一个独立和非官方的包。使用风险自负。