如何使用 cURL 获取目标 URL? [英] How can I get the destination URL using cURL?

查看:25
本文介绍了如何使用 cURL 获取目标 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I get the destination URL using cURL when the HTTP status code is 302?

<?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);

这篇关于如何使用 cURL 获取目标 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆