我正在尝试建立一种无需使用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 回答
- 1 回答
- 0 关注
- 179 浏览
添加回答
举报
0/150
提交
取消