我正在嘗試建立一種無需使用Twitter的API即可獲取某人的Twitter ID的工具。在我的示例中,我的用戶名是quixthe2nd。因此,如果您在此鏈接中輸入某人的Twitter用戶名:https://twitter.com/quixthe2nd/profile_image?size=original它將重定向到:https://pbs.twimg.com/profile_images/1116692743361687553/0P-dk3sF.jpgID為1116692743361687553。在后面列出https://pbs.twimg.com/profile_images/。我的代碼是:$url = "https://twitter.com/quixthe2nd/profile_image?size=original";function get_redirect_target($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $headers = curl_exec($ch); curl_close($ch); // Check if there's a Location: header (redirect) if (preg_match('/^Location: (.+)$/im', $headers, $matches)) return trim($matches[1]); // If not, there was no redirect so return the original URL // (Alternatively change this to return false) return $url;}function get_redirect_final_target($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // set referer on redirect curl_exec($ch); $target = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); curl_close($ch); if ($target) return $target; return false;} $s = $target; if(preg_match('/profile_images\/\K[^\/]+/',$s,$matches)) { print($matches[0]); }我的代碼什么都不輸出。我將如何通過PHP來抓取呢?
- 1 回答
- 0 關(guān)注
- 200 瀏覽
添加回答
舉報(bào)
0/150
提交
取消