我在尝试使用 curl 复制的浏览器请求时遇到一些问题。我目前正在从事一个大学项目并且被卡住了。
我正在尝试将浏览器请求复制到以下 URL: http: //vm.tiktok.com/e9VDx8/当我在浏览器中访问该页面时,我被重定向到包含视频和其他一些内容的页面。当我尝试使用 CURL 时,我看到了 404 page not found 错误。我的 curl 请求如下所示。
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, $USER_AGENT);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt( $ch, CURLOPT_COOKIEJAR, realpath('./cookies.txt') );
curl_setopt( $ch, CURLOPT_COOKIEFILE, realpath('./cookies.txt') );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
我在浏览器中查看了原始 URL 的标题并尝试将它们复制粘贴到 curl 中,但我仍然得到 404 页面。如果我将浏览器请求复制为来自 chrome 开发人员工具的 curl 请求并在终端中运行它,它工作正常。
curl "http://vm.tiktok.com/e9VDx8/" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: en-US,en;q=0.9,fr-CA;q=0.8,fr;q=0.7" -H "Cookie: _ga=GA1.2.213365735.1552156986; _gid=GA1.2.1717226934.1552319684; tt_webid=6667489497775638018" --compressed
任何帮助将非常感激。我难住了。