【发布时间】:2021-07-28 04:48:32
【问题描述】:
如果我访问网站https://example.com/a/abc?name=jack,我会被重定向到https://example.com/b/uuid-123。我只想要结束 URL,即https://example.com/b/uuid-123。 https://example.com/b/uuid-123 的内容约为 1mb,但我对内容不感兴趣。我只想要重定向的 URL 而不是内容。我怎样才能获得重定向的 URL,而不必加载 1mb 的内容,这会浪费我的带宽和时间。
我在 stackoverflow 上看到了一些关于重定向的问题,但没有关于如何不加载内容的问题。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/a/abc?name=jack');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$end_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
echo('End URL is ' . $end_url);
【问题讨论】:
-
是什么阻止了您使用
curl_setopt($ch, CURLOPT_NOBODY, true);检索标头?然后,您可以解析location标头的标头。 -
我不知道这存在。谢谢