DEV Community

alireza Programmer
alireza Programmer

Posted on

how to create tiktok downloader with php?

step one

create Tiktok Class file as Ticktok.php

<?php

class Tiktok
{

    static function downloadWithLink($post_link){
        $req = self::request($post_link);
        if (!$req) return false;

        if (!$req->status) return false;

        return $req->download_url;

    }

    static function request($link){
        // Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://www.tiktokdownloader.org/check.php?v=' . $link);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

        curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

        $headers = array();
        $headers[] = 'Connection: keep-alive';
        $headers[] = 'Sec-Ch-Ua: ';
        $headers[] = 'Accept: */*';
        $headers[] = 'X-Requested-With: XMLHttpRequest';
        $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
        $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36';
        $headers[] = 'Sec-Ch-Ua-Platform: Windows';
        $headers[] = 'Sec-Fetch-Site: same-origin';
        $headers[] = 'Sec-Fetch-Mode: cors';
        $headers[] = 'Sec-Fetch-Dest: empty';
        $headers[] = 'Referer: https://www.tiktokdownloader.org/';
        $headers[] = 'Accept-Language: en-US,en;q=0.9';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        $error = curl_errno($ch);
        curl_close($ch);
        if ($error) {
            return false;
        }else{
            return json_decode($result);
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

step two

make index.php file

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>TikTok Downloader</title>
</head>
<body>

<form method="post" action="api.php">
    <label for="post_link">
        Enter post link
    </label>

    <input id="post_link" name="post_link" type="text" placeholder="https://www.tiktok.com/@bloopers6090/video/7036954064382561583" required>

    <input type="submit">
</form>


</body>
</html>
Enter fullscreen mode Exit fullscreen mode

step 3

create api.php file

<?php
include "Tiktok.php";
$download_link = '';
$error = false;
if (isset($_POST["post_link"])){
    $download_link = Tiktok::downloadWithLink($_POST["post_link"]);
    if (!$download_link){
        $error = true;
    }
}

?>


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Result</title>
</head>
<body>
<?php

if (!$error){
    ?>
    <a href="<?= $download_link ?>">download link</a>
<?php
}
?>
<br>
<a href="">back to home page</a>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode
  1. now go to localhost/{project_folder_name}/index.php
  2. paste your ticktok post link
  3. submit
  4. click on download link

tanks for read.

you can subscribe me in youtube
youtube link : https://www.youtube.com/channel/UCe6dqJXYW5OfLHLz79dUq7g

Top comments (1)

Collapse
 
flock_robert profile image
Robert Flock

This just tell third party create tiktok download the url for you, it's not directly from tiktok.