D
D
denford-art2020-09-22 06:52:07
PHP
denford-art, 2020-09-22 06:52:07

Why does the script not see the link?

I decided to try the code from the article on Habré ( https://habr.com/ru/post/511796/ )
And when I tried to check the script, an error appeared that it could not find the link, what's the problem? Maybe the API has changed?

<?php

class TikTok {

    /**
     * @var string
     */
    public $url;

    public function __construct (string $url) {
        $this->url = $url;
    }

    /**
     * @return null|string
     * @throws Exception
     */
    public function getUrl () :? string {
        // Получаем код страницы
        $responseHtml = $this->cUrl($this->url);

        // Находим ссылку на видео
        if (!preg_match('/contentUrl\\":\\"(.*?)\\",\\"embedUrl/ui', $responseHtml, $mInterUrl))
            throw new \Exception('Ссылка не найдена!');

        // Отправляем запрос и в ответе получаем видео ввиде bytecode
        if (!$respByteVideo = $this->cUrl($mInterUrl[1]))
            throw new \Exception('Запрос не обработался!');

        // Чтобы регулярное выражение начало искать, нужно перевести в формат utf-8
        $strByteVideo = mb_convert_encoding($respByteVideo, 'UTF-8', 'auto');

        // Ищем специальный id видео, чтобы на его основе построить запрос
        if (!preg_match('/vid:(.*?)%/sui', $strByteVideo, $mVideoId))
            throw new \Exception('id video не было найдено!');

        // Уберём лишние символы
        $url = str_replace("\0", '', $mVideoId[1]);

        // Строим ссылку на получения видео без водяного знака
        $url = "https://api.tiktokv.com/aweme/v1/playwm/?video_id=$url";

        // Так как эта redirect на другую ссылку к видео, то пытаемся получить ее после redirect
        return $this->redirectUrl($url);
    }

    /**
     * Получение url адреса после redirect
     *
     * @param string $url
     * @return null|string
     */
    private function redirectUrl (string $url) :? string {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        $headers = get_headers($url, 1);
        return $headers['Location'] ?? NULL;
    }

    /**
     * @param string $url
     * @return null|string
     */
    private function cUrl (string $url) :? string {
        $user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36';
        $curl       = curl_init($url);
        curl_setopt_array($curl, [
            CURLOPT_URL            => $url,
            CURLOPT_RETURNTRANSFER => TRUE,
            CURLOPT_FOLLOWLOCATION => TRUE,
            CURLOPT_USERAGENT      => $user_agent,
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_TIMEOUT        => 10,
        ]);

        $response = curl_exec($curl);

        if ($response === FALSE) {
            curl_close($curl);
            return NULL;
        }

        $httpCode = (int)curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        if ($httpCode !== 200)
            return NULL;

        return $response;
    }
}

$TikTok = new TikTok();
echo $TikTok->getUrl();


Tried already more than a dozen links in the object

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2020-09-22
@denford-art

Because tik tok fixed this crap. They don't have a public API.
And the ID used to be stored in the video binary. They took him from there. If you try to download an old video, you will get mb more. Definitely not new. Until there is no other way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question