الحصول على مقاطع فيديو من Tik Tok بدون علامة مائية

مساء الخير لجميع عشاق habr. في هذه المقالة ، أريد أن أشاركك كيف يمكنك الحصول على فيديو Tik Tok بدون علامة مائية باستخدام لغة مثل PHP.



حاليًا ، تكتسب Tik Tok شعبية ولن يكون من اللائق عدم كتابة مقال صغير عنه ، وبالتالي ، كلمات أقل ، المزيد من الإجراءات.



لنقم بإنشاء فئة تسمى TikTok ، وسوف تحتوي على ثلاث طرق وخاصية واحدة.



طرق:



  • cUrl (طلب الضفيرة)
  • redirectUrl (الحصول على الرابط بعد إعادة التوجيه)
  • getUrl (الحصول على رابط الفيديو)


الخصائص:



  • URL العام $؛


لنقم بإنشاء مُنشئ لتمرير عنوان url.



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


إن cUrl. نرسل طلبًا إلى الخادم ونتلقى ردًا.



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;
}


طريقة RedirectUrl



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;
}


طريقة GetUrl.

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);
}


دعونا ننشئ كائنًا بناءً على الفصل ونمرر رابطًا إليه.



$TikTok = new TikTok('https://www.tiktok.com/@sonyakisa8/video/6828487583694163205?lang=ru');
echo $TikTok->getUrl();


كل شيء جاهز.



أمثلة:





كود كامل



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;
	}
}



All Articles