Skip to content

Commit a447ae4

Browse files
committed
tiktok api get user video list
1 parent dccb143 commit a447ae4

5 files changed

Lines changed: 319 additions & 1 deletion

File tree

tiktokapi/index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
$scopes = array( // a list of approved scopes by tiktok for your app
1717
'user.info.basic',
1818
'user.info.profile',
19-
'user.info.stats'
19+
'user.info.stats',
20+
'video.list'
2021
);
2122

2223
// get TikTok login url

tiktokapi/video/list.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<html>
2+
<head>
3+
<title>
4+
Bubbles | TikTok API PHP SDK Videos
5+
</title>
6+
7+
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
8+
9+
<link rel="shortcut icon" href="../assets/favicon.ico" />
10+
11+
<style>
12+
body {
13+
font-family: 'Courier New';
14+
background: #f0f2f5;
15+
}
16+
17+
a {
18+
text-decoration: none;
19+
}
20+
21+
#show_more_container {
22+
display: grid;
23+
width: 100%;
24+
gap: 20px 16px;
25+
grid-template-columns: repeat( auto-fill, minmax( 200px, 1fr ) );
26+
}
27+
28+
.tiktok-card {
29+
box-shadow: 0 2px 4px rgba( 0, 0, 0, .1 ), 0 8px 16px rgba( 0, 0, 0, .1 );
30+
border-radius: 5px;
31+
}
32+
33+
.tiktok-card img {
34+
width: 100%;
35+
display: block;
36+
border-top-left-radius: 5px;
37+
border-top-right-radius: 5px;
38+
}
39+
40+
.tiktok-card-title {
41+
width: 100%;
42+
height: 50px;
43+
overflow-y: scroll;
44+
padding: 5px;
45+
box-sizing: border-box;
46+
font-weight: bold;
47+
font-size: 14px;
48+
}
49+
50+
.tiktok-card-info {
51+
font-size: 12px;
52+
padding: 5px;
53+
border-top: 1px solid #e3e3e3;
54+
}
55+
56+
.tiktok-card-actions {
57+
font-size: 12px;
58+
padding: 5px;
59+
border-top: 1px solid #e3e3e3;
60+
}
61+
62+
.tiktok-button {
63+
background: #eb4863;
64+
color: #fff;
65+
border-radius: 5px;
66+
font-size: 18px;
67+
cursor: pointer;
68+
text-align: center;
69+
font-weight: bold;
70+
padding: 5px;
71+
}
72+
73+
.tiktok-button:hover {
74+
background: #E61A3C;
75+
}
76+
77+
.tiktok-button:active {
78+
box-shadow: inset 0 0 20px #ffffff;
79+
}
80+
81+
#show_more_button {
82+
width: 100%;
83+
padding: 20px;
84+
text-align: center;
85+
color: rgb( 0, 102, 204 );
86+
cursor: pointer;
87+
font-weight: bold;
88+
box-sizing: border-box;
89+
}
90+
</style>
91+
92+
<script>
93+
var cursor = '';
94+
95+
document.addEventListener( 'DOMContentLoaded', function() { // on page load
96+
document.getElementById( 'show_more_button' ).addEventListener( 'click', function() {
97+
showMore();
98+
} );
99+
} );
100+
101+
showMore();
102+
103+
function showMore() {
104+
const xmlhttp = new XMLHttpRequest();
105+
106+
xmlhttp.onload = function() {
107+
var resp = JSON.parse( this.responseText );
108+
109+
cursor = resp.cursor;
110+
111+
document.getElementById( 'show_more_container' ).insertAdjacentHTML( 'beforeend', resp.html );
112+
}
113+
114+
xmlhttp.open( 'GET', 'list_show_more.php?cursor=' + cursor );
115+
xmlhttp.send();
116+
}
117+
</script>
118+
</head>
119+
<body>
120+
<div id="show_more_container">
121+
122+
</div>
123+
<div id="show_more_button">
124+
SHOW MORE
125+
</div>
126+
</body>
127+
</html>

tiktokapi/video/list_show_more.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
require_once '../defines.php';
3+
require_once '../vendor/autoload.php'; // composer
4+
//require_once '../tiktok-api-php-sdk/src/TikTok/autoload.php'; // not composer
5+
6+
use TikTok\Video\Video;
7+
use TikTok\Request\Params;
8+
use TikTok\Request\Fields;
9+
10+
$config = array( // instantiation config params
11+
'access_token' => '<USER_ACCESS_TOKEN>'
12+
);
13+
14+
// instantiate the video
15+
$video = new Video( $config );
16+
17+
$params = array( // customize params for the request
18+
'max_count' => 10 // customize how many videos we want in each request (max is 20)
19+
);
20+
21+
if ( $_GET['cursor'] ) {
22+
$params['cursor'] = $_GET['cursor'];
23+
}
24+
25+
$fields = array( // customize fields for the videos
26+
Fields::ID,
27+
Fields::CREATE_TIME,
28+
Fields::TITLE,
29+
Fields::COVER_IMAGE_URL,
30+
Fields::SHARE_URL,
31+
Fields::VIDEO_DESCRIPTION,
32+
Fields::DURATION,
33+
Fields::HEIGHT,
34+
Fields::WIDTH,
35+
Fields::TITLE,
36+
Fields::EMBED_HTML,
37+
Fields::EMBED_LINK,
38+
Fields::LIKE_COUNT,
39+
Fields::COMMENT_COUNT,
40+
Fields::SHARE_COUNT,
41+
Fields::VIEW_COUNT
42+
);
43+
44+
// get video list (params and fields can both be omitted for default functionality)
45+
$videoList = $video->getList( $params, $fields );
46+
47+
ob_start();
48+
49+
?>
50+
51+
<?php foreach ( $videoList['data']['videos'] as $video ) : // loop over video from response ?>
52+
<div class="tiktok-card">
53+
<img src="<?php echo $video['cover_image_url']; ?>" />
54+
<div class="tiktok-card-title">
55+
<?php echo $video['title']; ?>
56+
</div>
57+
<div class="tiktok-card-info">
58+
<div>
59+
<b><?php echo number_format( $video['view_count'] ); ?></b> views
60+
</div>
61+
<div>
62+
<b><?php echo number_format( $video['like_count'] ); ?></b> likes
63+
</div>
64+
<div>
65+
<b><?php echo number_format( $video['comment_count'] ); ?></b> comments
66+
</div>
67+
<div>
68+
<b><?php echo number_format( $video['share_count'] ); ?></b> shares
69+
</div>
70+
</div>
71+
<div class="tiktok-card-actions">
72+
<a href="<?php echo $video['share_url']; ?>" target="_blank">
73+
<div class="tiktok-button">
74+
Watch on TikTok
75+
</div>
76+
</a>
77+
</div>
78+
</div>
79+
<?php endforeach ; ?>
80+
81+
<?php
82+
83+
echo json_encode( array(
84+
'cursor' => $videoList['cursor_next'] ? $videoList['cursor_next'] : '',
85+
'html' => ob_get_clean()
86+
) );

tiktokapi/video_list.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
require_once 'defines.php';
3+
require_once 'vendor/autoload.php'; // composer
4+
//require_once 'tiktok-api-php-sdk/src/TikTok/autoload.php'; // not composer
5+
6+
use TikTok\Video\Video;
7+
use TikTok\Request\Params;
8+
use TikTok\Request\Fields;
9+
10+
$config = array( // instantiation config params
11+
'access_token' => USER_ACCESS_TOKEN
12+
);
13+
14+
// instantiate the video
15+
$video = new Video( $config );
16+
17+
$params = array( // customize params for the request
18+
'max_count' => 5 // customize how many videos we want in each request (max is 20)
19+
);
20+
21+
$fields = array( // customize fields for the videos
22+
Fields::ID,
23+
Fields::CREATE_TIME,
24+
Fields::TITLE,
25+
Fields::COVER_IMAGE_URL,
26+
Fields::SHARE_URL,
27+
Fields::VIDEO_DESCRIPTION,
28+
Fields::DURATION,
29+
Fields::HEIGHT,
30+
Fields::WIDTH,
31+
Fields::TITLE,
32+
Fields::EMBED_HTML,
33+
Fields::EMBED_LINK,
34+
Fields::LIKE_COUNT,
35+
Fields::COMMENT_COUNT,
36+
Fields::SHARE_COUNT,
37+
Fields::VIEW_COUNT
38+
);
39+
40+
// get video list (params and fields can both be omitted for default functionality)
41+
$videoList = $video->getList( $params, $fields );
42+
43+
echo '<pre>';
44+
print_r( $videoList );
45+
46+
if ( $videoList['cursor_next'] ) { // we have more videos
47+
// add on cursor for the next request
48+
$params['cursor'] = $videoList['cursor_next'];
49+
50+
// get video list with new cursor (next page)
51+
$videoList = $video->getList( $params, $fields );
52+
53+
print_r( '---------NEXT---------' );
54+
print_r( $videoList );
55+
}
56+
?>

tiktokapi/video_query.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
require_once 'defines.php';
3+
require_once 'vendor/autoload.php'; // composer
4+
//require_once 'tiktok-api-php-sdk/src/TikTok/autoload.php'; // not composer
5+
6+
use TikTok\Video\Video;
7+
use TikTok\Request\Params;
8+
use TikTok\Request\Fields;
9+
10+
$config = array( // instantiation config params
11+
'access_token' => USER_ACCESS_TOKEN
12+
);
13+
14+
// instantiate the video
15+
$video = new Video( $config );
16+
17+
$videoIds = array( // videos ids we want info (max 20 ids)
18+
'<TIKTOK_VIDEO_ID>',
19+
'<TIKTOK_VIDEO_ID>',
20+
'<TIKTOK_VIDEO_ID>',
21+
// ...
22+
);
23+
24+
$fields = array( // customize fields for the videos
25+
Fields::ID,
26+
Fields::CREATE_TIME,
27+
Fields::TITLE,
28+
Fields::COVER_IMAGE_URL,
29+
Fields::SHARE_URL,
30+
Fields::VIDEO_DESCRIPTION,
31+
Fields::DURATION,
32+
Fields::HEIGHT,
33+
Fields::WIDTH,
34+
Fields::TITLE,
35+
Fields::EMBED_HTML,
36+
Fields::EMBED_LINK,
37+
Fields::LIKE_COUNT,
38+
Fields::COMMENT_COUNT,
39+
Fields::SHARE_COUNT,
40+
Fields::VIEW_COUNT
41+
);
42+
43+
// get video list (fields can be omitted for default functionality)
44+
$videoQuery = $video->query( $videoIds, $fields );
45+
46+
echo '<pre>';
47+
print_r( $videoQuery );
48+
?>

0 commit comments

Comments
 (0)