Forum Moderators: coopster
function getFile($url, $getInfo=false) {
$t = false;
if (strpos($url, 'http') === 0) {
$ch = curl_init(filter_var($url, FILTER_VALIDATE_URL));
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$t = curl_exec($ch);
// not actually used here, it's part of the general script that I sometimes use
// to make sure that an external file is responding
if ($getInfo && $t) {
$arr = curl_getinfo($ch);
$t = $arr['http_code'] === 200 ? $arr[$getInfo] : false;
}
curl_close($ch);
}
return $t;
}
$url = 'https://vm.tiktok.com/AbCdEfGhI/?k=1';
$data = getFile($url);
echo $data; [edited by: not2easy at 2:46 am (utc) on May 13, 2022]
[edit reason] readability [/edit]
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); function getFile($url, $getInfo=false, $getRedirect=false) {
$t = false;
if (strpos($url, 'http') === 0) {
$ch = curl_init(filter_var($url, FILTER_VALIDATE_URL));
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($getRedirect) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
}
$t = curl_exec($ch);
if ($getInfo && $t) {
$arr = curl_getinfo($ch);
$t = $arr['http_code'] === 200 ? $arr[$getInfo] : false;
}
curl_close($ch);
}
return $t;
}
// not a real link, this is just a dummy for the thread
$url = 'https://vm.tiktok.com/AbCdEfGhI/?k=1';
$data = getFile($url, false, true);
$arr = explode("\n", $data);
$arr[4] = ltrim($arr[4], 'Location: ');
$final_url = 'https://www.tiktok.com/oembed?url=' . $arr[4];
// should return the response data
$newData = getFile($final_url); this option should have done both GET requests in a single curl_exec.
was $getRedirect true when you called this function?
curl_setopt($ch, CURLOPT_HEADER, 1); // added $http_code=200
function getFile($url, $getInfo=false, $http_code=200) {
$t = false;
if (strpos($url, 'http') === 0) {
$ch = curl_init(filter_var($url, FILTER_VALIDATE_URL));
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$t = curl_exec($ch);
if ($getInfo && $t) {
$arr = curl_getinfo($ch);
// changed "=== 200" to "=== $http_code"
$t = $arr['http_code'] === $http_code ? $arr[$getInfo] : false;
}
curl_close($ch);
}
return $t;
}
// not a real link, this is just a dummy for the thread
$url = 'https://vm.tiktok.com/AbCdEfGhI/?k=1';
// defining $getInfo as 'redirect_url' and $http_code as 301
// this should return the final destination link
$data = getFile($url, 'redirect_url', 301);
$final_url = 'https://www.tiktok.com/oembed?url=' . $data;
// should return the final response data
$newData = getFile($final_url);