PHP抖音视频最新提取代码
研究了很多天,发现直接调用接口感觉不太行,所以只能采集HTML页面内容了。
下面直接附上代码吧
需要安装插件库
use GuzzleHttp\Client;
这里大家自行百度查找这个插件库吧
下面是代码
public function Video() { // 视频分享链接 $url = 'https://v.douyin.com/UYwNRAK/'; // 发送 GET 请求获取视频的 HTML 页面 $client = new Client([ 'verify' => false, 'allow_redirects' => [ 'max' => 0, ], ]); $response = $client->get($url); $html = $response->getBody()->getContents(); // 从 HTML 页面中提取视频的真实地址 $pattern = '/video\/(.*?)\//'; preg_match($pattern, $html, $matches); //拼接PC版地址 $pcUrl = "https://www.douyin.com/video/"; if (isset($matches[1])) { $video_url = $pcUrl.$matches[1]; } else { return '未找到视频地址'; } // 提取真实视频 $headers = [ 'cookie' => 'douyin.com; n_mh=XoDi_bSkJjNjXuJ1wDYlv4ldwjaX643aMZ3ct-LUBuY; passport_assist_user=CkHOiAxW7DPVkzSUwHFXlPOWPTndBZisSIHT4Y7YZnIyrj79kbtwg0qrp03a0_TrcKZauQqaYJJwkD-UyvDBY3gAORpICjxaND7jRScgUDoHf5ZAOjrtbqcIyYpgFElDUD0QgWw7WWi8DsU5XVmAddI-uaQQZsqaeAf0pZrOM9JIBJQQnMKqDRiJr9ZUIgEDS9nfMg%3D%3D; sso_uid_tt=199257f0818bf1ca098f6e7b23ff5bbb; sso_uid_tt_ss=199257f0818bf1ca098f6e7b23ff5bbb; toutiao_sso_user=d50cf18a6bc2eef4710c6d2bf2fe8eca; toutiao_sso_user_ss=d50cf18a6bc2eef4710c6d2bf2fe8eca; uid_tt=6437b6f4afa709a189575a49d99a28ee; uid_tt_ss=6437b6f4afa709a189575a49d99a28ee; sid_tt=b711fa12a4c693159784ced8fec69206; sessionid=b711fa12a4c693159784ced8fec69206; sessionid_ss=b711fa12a4c693159784ced8fec69206; store-region-src=uid; ttwid=1%7CquN0IBK7KSK-fmohDybqYHtKjvLepKzHlfZySXOlMl0%7C1678373545%7C2fbb952b99105dc8b7ccad56d653b4840fcb73f90a8f187cbedca8afbcfa357f; odin_tt=eb063b5e9c7cb092c048b9fcf36a7f91c8f38d9f72edafe90a60e686a2ae9670710da4907098c99287979236d258e5f0; __live_version__=%221.1.0.7804%22; s_v_web_id=verify_lhkesk8n_oJ4jNKCG_PS9j_4ozB_8I30_ObnqdZ85vyqE; LOGIN_STATUS=1; passport_csrf_token=1fe8a546129830c60049640293fbbf33; passport_csrf_token_default=1fe8a546129830c60049640293fbbf33; sid_ucp_sso_v1=1.0.0-KGJmM2M2YjA3OGZhMTE5MWEyMjJhNmY0ZDJjM2IyMTMzMjM0YjZmZGQKHwj3g5Hti_TfAxDFpviiBhjvMSAMMOO2r_gFOAZA9AcaAmxmIiBkNTBjZjE4YTZiYzJlZWY0NzEwYzZkMmJmMmZlOGVjYQ; ssid_ucp_sso_v1=1.0.0-KGJmM2M2YjA3OGZhMTE5MWEyMjJhNmY0ZDJjM2IyMTMzMjM0YjZmZGQKHwj3g5Hti_TfAxDFpviiBhjvMSAMMOO2r_gFOAZA9AcaAmxmIiBkNTBjZjE4YTZiYzJlZWY0NzEwYzZkMmJmMmZlOGVjYQ; sid_ucp_v1=1.0.0-KDY2OGIyZTYyNGQ5YjUzZTViMDA3YzhjZDEwYzg4MTc0YjI3NjExZGQKGwj3g5Hti_TfAxDGpviiBhjvMSAMOAZA9AdIBBoCbHEiIGI3MTFmYTEyYTRjNjkzMTU5Nzg0Y2VkOGZlYzY5MjA2; ssid_ucp_v1=1.0.0-KDY2OGIyZTYyNGQ5YjUzZTViMDA3YzhjZDEwYzg4MTc0YjI3NjExZGQKGwj3g5Hti_TfAxDGpviiBhjvMSAMOAZA9AdIBBoCbHEiIGI3MTFmYTEyYTRjNjkzMTU5Nzg0Y2VkOGZlYzY5MjA2; sid_guard=b711fa12a4c693159784ced8fec69206%7C1683886918%7C5184001%7CTue%2C+11-Jul-2023+10%3A21%3A59+GMT; download_guide=%223%2F20230512%22; publish_badge_show_info=%221%2C0%2C0%2C1683920285202%22; VIDE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hSRUVcbkVqQVFnZzUzZDNjdVpHOTFlV2x1TG1OdmJUQUtCZ2dxaGtqT1BRUURBZ05JQURCRkFpQWxqbGIrcmxqSUJQaWhcbnNIR1JlQ0kwUTRvM0huei9TY09rK2VqTFU5N3RHd0loQU8zSHdNVk5TemJyeHg3ZWg1TGYyaWlScHdvMmlKZzZcblZ2Wm1LMzdSVlI1VFxuLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLVxuIn0=; __ac_nonce=0645ff00000f75f4788f4; __ac_signature=_02B4Z6wo00f01FQurqgAAIDBg-iD0aqOo0xUD6oAAHFbCPW7..bnqd4GeYfz0C-Z2X8xGhJJ-bYcCD7KPAV166YWo85cXdcB5Gtp090nR0XZytb6ffusk844nWAwp.wqHPPi6tfl5UtEgSpW1b; passport_fe_beating_status=true; store-region=jp; FOLLOW_LIVE_POINT_INFO=%22MS4wLjABAAAAye4-wu2qZKx-KO0GROKB2FmKyf49fVMxKYUDXvZwEwfdsWY27ilG_WzKvbjeom5e%2F1684080000000%2F0%2F0%2F1684010722905%22; FOLLOW_NUMBER_YELLOW_POINT_INFO=%22MS4wLjABAAAAye4-wu2qZKx-KO0GROKB2FmKyf49fVMxKYUDXvZwEwfdsWY27ilG_WzKvbjeom5e%2F1684080000000%2F0%2F0%2F1684011322906%22; msToken=KhEOm2c2mvPPMtcWiszPHquy5A4Bj3hVWfLfWpq-qeVO8wUjMiyvy2UFRr5oJmxpuuVAeozA55F-GGxWZ0AW19XFA9xvvuSHKr27JfKsQb_wbXtKjkQEHZRnGZ0Ldm2Y; home_can_add_dy_2_desktop=%221%22; msToken=4JHnCUN-upd5zjVZH-TEKkr-xKHe0BzJs6Dar9NP1hhvpkyu8PuSP4gtdtfG844zvnieR_GfRbyd2BvKE6IuS0gNjP73XxN8SgwB_qEPOTLFk6XTmNZJ93rkpLk6E_oy; tt_scid=My71Yv1CSaW9.ELn18.o-CxCGsLxvsM.2DhK4tMDRqoIn0e0LVJwWbZ4.cLTEZh86dd5', 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', ]; $response = $client->get($video_url,[ 'headers' => $headers, ]); $video_data = $response->getBody()->getContents(); $decodedUrl = urldecode($video_data); $pattern = '/