Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit(0);
- error_reporting(E_ALL);
- define('PATH' , dirname(__FILE__) . DIRECTORY_SEPARATOR);
- require_once 'simple_html_dom.php';
- $engine = new Tiktok();
- $engine->run();
- class Tiktok {
- public $token = '';
- public $cookies = '';
- public $useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36';
- public $lang = 'ru';
- public $usernames = [];
- public $tags = [];
- public function __construct()
- {
- $this->cookies = PATH . 'cookies.txt';
- file_put_contents($this->cookies, '');
- }
- public function run()
- {
- $this->enter();
- }
- public function enter() {
- // open main tiktok page
- $this->curl('https://www.tiktok.com/ru');
- //open tranding
- $this->curl('https://www.tiktok.com/trending/?lang=' . $this->lang);
- //open discover
- $response = $this->curl('https://www.tiktok.com/discover?lang=' . $this->lang);
- $this->parse_usernames($response['content']);
- $this->parse_tags($response['content']);
- // print_r($this->usernames);
- // print_r($this->tags);
- $this->parse_tags_from_usernames();
- }
- public function parse_tags_from_usernames()
- {
- if (count($this->usernames) > 0) {
- foreach ($this->usernames as $username) {
- $response = $this->curl('https://www.tiktok.com/'.$username.'?lang=' . $this->lang);
- echo $response['content'];
- die();
- }
- }
- }
- public function parse_tags($html)
- {
- $dom = str_get_html($html);
- foreach($dom->find('._card_header_title') as $el)
- {
- $item = $el->innertext;
- if (substr($item, 0, 1) == '#') {
- $this->add_tag($item);
- }
- }
- }
- public function parse_usernames($html)
- {
- $dom = str_get_html($html);
- foreach($dom->find('a') as $a)
- {
- if (mb_strlen($a->href) > 0) {
- $tmp = explode('/', $a->href);
- if (count($tmp ) > 0) {
- foreach ($tmp as $item) {
- if (substr($item, 0, 1) == '@') {
- $this->add_username($item);
- break;
- }
- }
- }
- }
- }
- }
- public function add_username($username)
- {
- $username = trim($username);
- if (!in_array($username, $this->usernames)) {
- $this->usernames[] = $username;
- }
- }
- public function add_tag($tag)
- {
- $tag = trim($tag);
- if (!in_array($tag, $this->tags)) {
- $this->tags[] = $tag;
- }
- }
- public function curl($url)
- {
- $options = array(
- CURLOPT_RETURNTRANSFER => true, // return web page
- CURLOPT_HEADER => false, // don't return headers
- CURLOPT_FOLLOWLOCATION => true, // follow redirects
- CURLOPT_ENCODING => "", // handle all encodings
- CURLOPT_USERAGENT => $this->useragent, // who am i
- CURLOPT_AUTOREFERER => true, // set referer on redirect
- CURLOPT_CONNECTTIMEOUT => 30, // timeout on connect
- CURLOPT_TIMEOUT => 30, // timeout on response
- CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
- CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
- CURLOPT_COOKIEFILE => $this->cookies,
- CURLOPT_COOKIEJAR => $this->cookies,
- CURLOPT_HTTPHEADER => [
- // ':authority: www.tiktok.com',
- // ':method: GET',
- // ':path: /ru',
- // ':scheme: https',
- 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
- 'accept-encoding: gzip, deflate, br',
- 'accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6',
- 'cache-control: max-age=0',
- // 'sec-fetch-mode: navigate',
- // 'sec-fetch-site: same-origin',
- // 'sec-fetch-user: ?1',
- // 'upgrade-insecure-requests: 1',
- ]
- );
- $ch = curl_init( $url );
- curl_setopt_array( $ch, $options );
- $content = curl_exec( $ch );
- $err = curl_errno( $ch );
- $errmsg = curl_error( $ch );
- $header = curl_getinfo( $ch );
- curl_close( $ch );
- $header['errno'] = $err;
- $header['errmsg'] = $errmsg;
- $header['content'] = $content;
- return $header;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement