php - 如何使用 cURL 获取目标 URL?

标签 php html http curl

当 HTTP 状态码为 302 时,如何使用 cURL 获取目标 URL?

<?PHP
$url = "http://www.ecs.soton.ac.uk/news/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$status_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

if($status_code=302 or $status_code=301){
  $url = "";
  // I want to to get the destination url
}
curl_close($ch);
?>

最佳答案

你可以使用:

echo curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

关于php - 如何使用 cURL 获取目标 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1439040/

相关文章:

php - 统计php mysql中的多级营销(树)记录

php - ffmpeg淡化过滤器已安装但无法正常工作?

php - 如何在浏览器地址栏中使用 Laravel 命令?

php - 如何使用 chdir 切换到当前目录?

jQuery:无法选择所有输入,并且在按键时选择 'this' 并显示下一个元素

javascript - 在对象创建器中向 DOM 添加选项

javascript - 检查 localStorage 的值但显示未定义

python - 在 python 中解码 HTTP 数据包内容,如 wireshark 中所见

http - http header 中是否允许多个线性空白

Android HTTP 请求奇怪的 404 未找到问题