Retrieve Video List from the official TikTok APIs

TikTok

The TikTok PHP SDK has been updated, and the new v0.2 brings support for retrieving the User’s Videos.

If you’re new to the TikTok PHP SDK, please read my previous tutorial on how to log in and receive the Access Token. In that article, I also provide a bit of backstory and more general information about the TikTok Login Kit APIs for Web.

Once you’re all caught up and know how to log in and retrieve the access Token, retrieving the Videos is blazing fast.

The easiest way is to use the getUserVideoPages helper method, since it provides built-in pagination.

$videos = $_TK->getUserVideoPages();

This will return an array that contains all the videos of the logged in user. Watch out because this will keep looping and calling the APIs for all the videos you have. So you might want to add the max_pages parameter. For example to limit to the latest 10 pages only:

$videos = $_TK->getUserVideoPages(10);

If you want more control over the pagination, you can use the higher-level getUserVideos which allows you to manage your own pagination via the cursor and num_results parameters. For example, let’s say you only want the latest video:

$videos = $_TK->getUserVideos(0, 1);

The pagination info is provided in the cursor and has_more returned values. Here’s how you can implement your own pagination:

$videos = [];
$cursor = 0;
$count_pages = 0;
$max_pages = 3;
while ($vids = $this->getUserVideos($cursor)) {
$count_pages++;
if ($count_pages > $max_pages && $max_pages > 0) {
break;
}
foreach ($vids['videos'] as $v) {
$videos[] = $v;
}
if ($vids['has_more']) {
$cursor = $vids['cursor'];
} else {
break;
}
}

Once you have your $videos you can use the simple getter methods, e.g. getCoverImageURL to get all the available parameters.

Here’s a quick sample, populating a table

foreach ($videos as $v) {
$trs[] = <<<HTML
<tr>
<td width="100"><img src="{$v->getCoverImageURL()}" style="width:100%"></td>
<td width="100">
<strong>ID</strong>: {$v->getID()}<br /><br />
<strong>URL</strong>: {$v->getShareURL()}<br /><br />
<strong>Caption</strong>: {$v->getVideoDescription()}
</td>
</tr>
HTML;
}

All done! If you have any suggestion on the class or its implementation please feel free to reach out on GitHub or Twitter.

Co-founder @Lnk.Bio — Developing stuff on the Web since 2003

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

look ma, no database connection pools ;-)

A Guide to Automated Testing with Drupal and Applitools

From Mechanical Engineer to developer: What I have learned and how it can help you become a better…

What is localization testing and why is it important?

Remote Code Execution — Bypassing WAF and Filters

Know this before you enter Web Development!

Resume Screening with Natural Language Processing in Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrea Olivato

Andrea Olivato

Co-founder @Lnk.Bio — Developing stuff on the Web since 2003

More from Medium

Old android phone as a Discord bot server.

Collect the list of node dependencies from a node project

Websockets made Simple

How to Google Signin within a Ionic/Vue.js APP using Firebase