php使用curl请求返回302(30x)

  web站点已开始使用https,但是项目代码里还有遗留的http,由此有了新发现。

  先贴代码:

<?php
$url = "http://www.baidu.com";
$resultStr = httpRequest($url);
echo $resultStr;

function httpRequest($url, $timeout = 10)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0');
    $rawResult = @curl_exec($ch);
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
    curl_close($ch);

    if ($curl_errno > 0) {
        return "Curl Error ($curl_errno): $curl_error\n";
    } else {
        return $rawResult;
    }
}

?>

  发现对于能使用https的站点,用浏览器访问时比如http://www.bai.com,会自动跳转至https://www.baidu.com,

而php使用curl时请求http://ww.baidu.com时,只会获取到302 Found(30x),如下图。

这个差异的原因是cURL会话句柄没有设置选项 CURLOPT_FOLLOWLOCATION ,即增加

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

这个选项在官方文档中的说明:

设置之后请求http://www.baidu.com就能成功请求到https://www.baidu.com了:

浏览器对于302、307等30x是有做重定向处理的:

总结,所谓的“新发现”其实是对curl不熟悉而有的“新了解”。

以上。

参考博客:https://evertpot.com/curl-redirect-requestbody/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用curl发送HTTP请求时,如果服务器返回的状态码是302,意味着发生了重定向。重定向是指服务器将请求转发到另一个URL。引用中提到,当使用curl请求http://ww.baidu.com时,只会获取到302 Found状态码。 为了获取重定向后的内容,可以使用以下方法进行设置和处理: 1. 初始化curl并设置URL: ``` $ch = curl_init($url); ``` 2. 设置请求头信息: ``` $headers = array( "Content-type: text/xml;charset=\"utf-8\"", "Accept: text/html, application/xml;q=0.9, application/xhtml xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1", "Cache-Control: no-cache", "Pragma: no-cache" ); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); ``` 3. 设置curl选项,包括返回结果字符串、支持gzip压缩和自动跟随重定向: ``` curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_ENCODING, "gzip"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 是否抓取跳转后的页面 ``` 4. 执行curl请求并获取结果: ``` $response = curl_exec($ch); ``` 通过以上设置,可以获取到重定向后的内容。引用中给出了使用curl获取302跳转之后内容的示例代码。 总结回答,当使用curl发送HTTP请求时,如果返回的状态码是302,说明发生了重定向。可以通过设置curl选项来获取重定向后的内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [php使用curl请求返回30230x)](https://blog.csdn.net/weixin_34453486/article/details/115411946)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [curl返回302 found问题相关](https://blog.csdn.net/kangaroo835127729/article/details/24303111)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值