liballab/tiktok-php

PHP TikTok爬虫

v1.9.16 2021-03-30 06:58 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...'
])

如果你没有设置,默认情况下,爬虫将尝试为你生成以下cookie

verifyFp, tt_webid, and tt_webid_v2

然而,在大多数情况下,fp令牌最终会成为触发验证码的原因。

Cookie文件

v1.8.0版本中,爬虫现在默认设置cookie,这意味着你必须为cookies.json文件提供可写权限,然后将其设置在配置中(请参见下面的示例配置)。

需要禁用cookie?在配置中设置'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的方法。

  1. 你可以使用nodejs,这个包会检查你是否安装了它,如果没有,它会尝试安装它。

  2. 你可以使用我亲自构建的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

如果没有错误,你会注意到两件事

  1. $scraper->error被设置为false
  2. 你调用的方法不会返回false

法律

此存储库及其内容与TikTok或其任何附属公司或子公司没有任何关联、授权、维护、赞助或认可。这是一个独立和非官方的包。使用风险自担。