使用 cURL 获取 TikTok final post URL

Get TikTok final post URL using cURL

我正在寻找 cURL 一个 TikTok post URL 并跟踪每个人 URL 它经历过。

在这个例子中(https://vm.tiktok.com/ZMeh1yKUQ/) I'm receiving https://m.tiktok.com/v/6841927751578946822.html instead of final url in browser: https://www.tiktok.com/@ibruno_maciel/video/6841927751578946822

    function getWebPage($url, $redirectcallback = null){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");

    $html = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($http_code == 301 || $http_code == 302) {
        list($httpheader) = explode("\r\n\r\n", $html, 2);
        $matches = array();
        preg_match('/(Location:|URI:)(.*?)\n/', $httpheader, $matches);
        $nurl = trim(array_pop($matches));
        $url_parsed = parse_url($nurl);
        if (isset($url_parsed)) {
            if($redirectcallback){ // callback
                 $redirectcallback($nurl, $url);
            }
            $html = getWebPage($nurl, $redirectcallback);
        }
    }
    return $html;
}

function trackAllLocations($newUrl, $currentUrl){
    echo $currentUrl.' ---> '.$newUrl."\r\n";
}

getWebPage('https://vm.tiktok.com/ZMeh1yKUQ/', 'trackAllLocations');

有什么建议吗?

不是 100% 确定原因,但如果您调试 headers 发回以检查它想要重定向的方式...

对于 301 响应...

Location: https://m.tiktok.com/...

对于 302...

location: https://www.tiktok.com/@ib...

所以你的正则表达式没有找到 Location,一个快速的解决方法是使搜索不区分大小写(使用 /i 标志)...

preg_match('/(Location:|URI:)(.*?)\n/i', $httpheader, $matches);

<?php

function request($uri = '')
{
  $uri = isMW($uri) ? get_headers($uri, 1)['Location'] : $uri;

  $curl = curl_init();

  curl_setopt_array($curl, [
    CURLOPT_URL => $uri,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => [
      'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36',
    ],
  ]);

  $response = curl_exec($curl);

  curl_close($curl);

  return $response;
}

// check mobile link ->

function isMW($uri)
{
  $pattern = '/vm./';

  return preg_match($pattern, $uri, $e, PREG_OFFSET_CAPTURE);
}

echo request('https://vm.tiktok.com/ZMeh1yKUQ/');