-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearchLive.php
More file actions
81 lines (68 loc) · 3.42 KB
/
searchLive.php
File metadata and controls
81 lines (68 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require __DIR__.'/vendor/autoload.php';
// API Configuration
$debug = false; // Debug mode
$licenseKey = ''; // Your own unique license key, which can be purchased here (https://evelode.com/downloads/tiktok-rest-api/)
// Request parameters
$keyword = ''; // Any text or keyword.
// Initialize TikTok REST API class
$tiktok = new \TikTokRESTAPI\TikTok($licenseKey, $debug);
// By default we use caching system with 3600 seconds window to speed up similar requests to API.
// If you want to disable caching, you can set 0 here.
$tiktok->setCacheTimeout(3600);
try {
if (empty($keyword)) {
getKeyword:
// Any text or keyword.
// This is an example how to get $keyword from Console/Terminal
echo 'Enter the text or keyword to search:';
$keyword = trim(fgets(STDIN));
}
search:
$resp = $tiktok->searchLive($keyword);
if ($resp->isOk() && $resp->hasTiktok() && $resp->getTiktok()->hasData() && count($resp->getTiktok()->getData()) > 0) {
$lives = $resp->getTiktok()->getData();
echo sprintf('Livestreams found by keyword "%s":', $keyword) . "\n";
foreach ($lives as $key => $l) {
$title = $l->getLives()->getAuthor()->getRoomTitle() !== "" ? $l->getLives()->getAuthor()->getRoomTitle() : "Empty Title";
$nickname = $l->getLives()->getAuthor()->getNickname();
$unique_id = $l->getLives()->getAuthor()->getUniqueId();
$room_id = $l->getLives()->getAuthor()->getRoomId();
echo sprintf('%s. %s | Author: %s (@%s) | Room ID: %s', $key + 1, $title, $nickname, $unique_id, $room_id) . "\n";
}
} else {
echo sprintf('Livestreams by keyword "%s" not found.', $keyword) . "\n";
}
echo 'Do you want to use search again?' . "\n";
echo '1 - Yes' . "\n";
echo '2 - No' . "\n";
echo 'Your choice:';
$choice = trim(fgets(STDIN));
if ($choice == 1) {
goto getKeyword;
}
} catch (TikTokRESTAPI\Exception\BadRequestException $e) {
// Request not performed because some data is missing or incorrect.
echo sprintf('BadRequestException Catched: %s', $e->getMessage()) . "\n";
} catch (TikTokRESTAPI\Exception\ForbiddenException $e) {
// Request failed due to invalid, expired, revoked license or access to API is restricted.
echo sprintf('ForbiddenException Catched: %s', $e->getMessage()) . "\n";
} catch (TikTokRESTAPI\Exception\NotFoundException $e) {
// Requested resource doesn't exist in TikTok.
echo sprintf('NotFoundException Catched: %s', $e->getMessage()) . "\n";
} catch (TikTokRESTAPI\Exception\ProxyAuthException $e) {
// Proxy auth data is missing or incorrect.
echo sprintf('ProxyAuthException Catched: %s', $e->getMessage()) . "\n";
} catch (TikTokRESTAPI\Exception\TooManyRequestsException $e) {
// Too many requests sent to TikTok.
echo sprintf('TooManyRequestsException Catched: %s', $e->getMessage()) . "\n";
} catch (TikTokRESTAPI\Exception\NetworkException $e) {
// Couldn't establish connection with REST API server
echo sprintf('NetworkException Catched: %s', $e->getMessage()) . "\n";
} catch (TikTokRESTAPI\Exception\TikTokException $e) {
// Invalid argument, missing or invalid data in request
echo sprintf('TikTokException Catched: %s', $e->getMessage()) . "\n";
} catch (Exception $e) {
echo sprintf('Exception Catched: %s', $e->getMessage()) . "\n";
echo var_dump($e->getTrace());
}