avatar
Untitled

Guest 68 15th Nov, 2019

                                           
                         <?php
class TiktokApi{
    public $api_url;


    public $cookies_dir;
    public $setUser;

    public function __construct($settings = [])
    {
        $this->api_url = 'https://api2.musical.ly/';
        $this->cookies_dir = __DIR__;
    }

    /**
     * SetCookieDir function to determine where to keep cookies
     * Normally the __construct () method is set to __DIR__.
     * Optional cookie directory can be changed.
     *! Important: must be used before the login () function!
    */

    public function setCookieDir($dir){
        $this->cookies_dir = $dir.'/';
    }

   /**
     * The first input request fails and sends a Captcha code in the printouts. This captcha code is encrypted with base64
     * Official base64_decode ($ image_base) to break the php page with the header () method should convert to jpg.
    */

    public function login($username,$password,$captcha = null)
    {
        $this->setUser = $this->cookies_dir.$username.'-cookie.txt';
        return $this->request('passport/user/login/?'.$this->requestArray(),array(
            'mix_mode'  => 1,
            'username'  => $username,
            'email'     => '',
            'mobile'    => '',
            'account'   => '',
            'password'  => $password,
            'captcha'   => $captcha,
        ));
    }

    /**
     * After completing the login process is the process of calling the cookie.
     * SetUser () function can be called directly if the user has successfully logged in.
     */

    public function SetUser($username)
    {
        return $this->setUser = $this->cookies_dir.$username.'-cookie.txt';
    }


    public function userInfo($user_id){
        return $this->request('aweme/v1/user/?user_id='.$user_id.'&'.$this->requestArray());
    }

    public function userMedias($user_id,$max_cursor = 0){
        return $this->request('aweme/v1/aweme/post/?max_cursor='.$max_cursor.'&user_id='.$user_id.'&count=10&retry_type=no_retry&'.$this->requestArray());
    }

    public function userFollowers($user_id,$max_time = null){
        if($max_time == null){ $max_time = (time() *1000); }
        return $this->request('aweme/v1/user/follower/list/?user_id='.$user_id.'&count=10&max_time='.$max_time.'&retry_type=no_retry&'.$this->requestArray());
    }

    public function userFollowing($user_id,$max_time = null){
        if($max_time == null){ $max_time = (time() *1000); }
        return $this->request('aweme/v1/user/following/list/?user_id='.$user_id.'&count=10&max_time='.$max_time.'&retry_type=no_retry&'.$this->requestArray());
    }

    public function getVideoDetail($video_id){
        return $this->request('aweme/v1/aweme/detail/?aweme_id='.$video_id.'&'.$this->requestArray());
    }

    public function getComments($video_id,$cursor = 0){
        return $this->request('aweme/v1/comment/list/?aweme_id='.$video_id.'&comment_style=2&digged_cid&insert_cids&?count=100&cursor='.$cursor.'&'.$this->requestArray());
    }

    public function follow($id){
        return $this->request('aweme/v1/commit/follow/user/?user_id='.$id.'&type=1&retry_type=no_retry&from=3&'.$this->requestArray());
    }

    public function Verify(){
        return $this->outRequest('https://verification-va.byteoversea.com/get?'.$this->requestArray());
    }

    public function getUserFollowers(){

    }

    public function PopularCategory(){
        return $this->request('aweme/v1/category/list/?'.$this->requestArray());
    }

    public function ForYou(){
        return $this->request('aweme/v1/feed/?count=25&offset=0&max_cursor=0&min_cursor=0&type=0&is_cold_start=1&pull_type=0&req_from&'.$this->requestArray());
    }

    public function searchUser($username){
        return $this->request('aweme/v1/discover/search/?cursor=0&keyword='.$username.'&count=10&type=1&hot_search=0&'.$this->requestArray());
    }

    public function searchHashtag($hashtag){
        return $this->request('aweme/v1/challenge/search/?cursor=0&keyword='.$hashtag.'&count=10&type=1&hot_search=0&'.$this->requestArray());
    }

    public function listHashtag($hashtagID,$cursor = 0){
        return $this->request('aweme/v1/challenge/aweme/?ch_id='.$hashtagID.'&count=20&offset=0&max_cursor=0&type=5&query_type=0&is_cold_start=1&pull_type=1&cursor='.$cursor.'&'.$this->requestArray());
    }


    public function headers(){
        return array(
            "Host" => $this->api_url,
            'X-SS-TC' => "0",
            'User-Agent' => "com.zhiliaoapp.musically/2018120331 (Linux; U; Android 9; vi_VN; Redmi Note 8 Pro; Build/PPR1.180610.011; Cronet/58.0.2991.0)",
            'Accept-Encoding' => "gzip",
            'Connection' => "keep-alive",
            'X-Tt-Token' => "",
            'sdk-version' => "1",
        );
    }

    public function request($endpoint,$post = null){


        $curl = curl_init();
        $options = array(
            CURLOPT_URL => $this->api_url.$endpoint,
            CURLOPT_USERAGENT => 'com.zhiliaoapp.musically/2018120331 (Linux; U; Android 9; vi_VN; Redmi Note 8 Pro; Build/PPR1.180610.011; Cronet/58.0.2991.0)',
            CURLOPT_REFERER => $this->api_url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_COOKIEFILE => $this->setUser,
            CURLOPT_COOKIEJAR => $this->setUser,
            CURLOPT_HTTPHEADER => $this->headers()
        );
        if($post){


            $options[CURLOPT_POST] = true;
            $options[CURLOPT_POSTFIELDS] = $post;
        }
        curl_setopt_array($curl,$options);
        $response = curl_exec($curl);
        curl_close($curl);
        return json_decode($response);
    }

    public function outRequest($page,$post = null){
        $curl = curl_init();
        $options = array(
            CURLOPT_URL => $page,
            CURLOPT_USERAGENT => 'com.zhiliaoapp.musically/2018120331 (Linux; U; Android 9; vi_VN; Redmi Note 8 Pro; Build/PPR1.180610.011; Cronet/58.0.2991.0)',
            CURLOPT_REFERER => $this->api_url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_COOKIEFILE => $this->setUser,
            CURLOPT_COOKIEJAR => $this->setUser,
            CURLOPT_HTTPHEADER => $this->headers()
        );
        if($post){
            $options[CURLOPT_POST] = true;
            $options[CURLOPT_POSTFIELDS] = $post;
        }
        curl_setopt_array($curl,$options);
        $response = curl_exec($curl);
        curl_close($curl);
        return json_decode($response);
    }

    public function requestArray(){
        $items = array(          
           'ts' => time(),
           'js_sdk_version' => '',
           'app_type' => 'normal',
           'app_language' => 'vi',
           'manifest_version_code' => '2018120331',
           '_rticket' => time(),
           'iid'    => '6759525368678057734',
           'channel' => 'googleplay',
           'language' => 'vi',
           'fp' => '',
           'device_type' => 'Redmi%20Note%208%20Pro',
           'resolution' => '1080*2134',
           'openudid' => 'c247ef762f4750ac',
           'update_version_code' => '2018120331',
           'sys_region' => 'VN',
           'os_api' => '28',
           'is_my_cn' => '0',
           'timezone_name' => 'Asia%2FHo_Chi_Minh',
           'dpi' => '440',
           'carrier_region' => 'VN',
           'ac' = > 'mobile',
           'device_id' => '6756152127692211713',
           'pass-route' => '1',
           'mcc_mnc' => '45204',
           'timezone_offset' => '25200',
           'os_version' => '9',
           'version_code' => '940',
           'carrier_region_v2' => '452',
           'app_name' => 'musical_ly',
           'ab_version' => '9.4.0',
           'version_name' => '9.4.0',
           'device_brand' => 'Redmi',
           'ssmix' => 'a',
           'pass-region' => '1',
           'build_number' => '9.4.0',
           'device_platform' => 'android',
           'region' => 'VN',
           'aid' => '1233',
           'as' => 'a1051a5c7ff04d6a5e2588',
           'cp' => 'a707d953f5e9caa0e1]eKi',
           'mas' => md5(sha1(time()))
);
          
       foreach ($items as $key => $item){
            $packet[] = $key.'='.$item;
        }
        $implode = implode('&',$packet);
        return $implode;
    }


}



?>
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes