How to obtain CURL request headers and response headers in php

小云云
Release: 2023-03-21 16:02:01
Original
8081 people have browsed it

This article mainly shares with you the methods of obtaining CURL request headers and response headers in PHP. I hope it can help you.

1. Get the response header from CURL

$oCurl = curl_init();
// 设置请求头, 有时候需要,有时候不用,看请求网址是否有对应的要求$header[] = "Content-type: application/x-www-form-urlencoded";$user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
curl_setopt($oCurl, CURLOPT_URL, $sUrl);
curl_setopt($oCurl, CURLOPT_HTTPHEADER,$header);
// 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文curl_setopt($oCurl, CURLOPT_HEADER, true);
// 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文curl_setopt($oCurl, CURLOPT_NOBODY, true);// 使用上面定义的 uacurl_setopt($oCurl, CURLOPT_USERAGENT,$user_agent);

//curl_setopt($oCurl, CURLOPT_REFERER,'http://www.baidu.com');设置referer
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
// 不用 POST 方式请求, 意思就是通过 GET 请求curl_setopt($oCurl, CURLOPT_POST, false);$sContent = curl_exec($oCurl);
// 获得响应结果里的:头大小$headerSize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
// 根据头大小去获取头信息内容$header = substr($sContent, 0, $headerSize);

// 获得响应结果里的:响应状态码

$httpCode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);

curl_close($oCurl);
Copy after login

2. Get the request header from curl

$oCurl = curl_init();
 curl_setopt($oCurl, CURLOPT_URL, "https://117.28.240.235:8002/ipcc/agent/login");
 curl_setopt($oCurl, CURLOPT_HTTPHEADER, $header);//关闭https验证
 curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);//至关重要,CURLINFO_HEADER_OUT选项可以拿到请求头信息curl_setopt($oCurl, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, 1);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $bodystr);$sContent = curl_exec($oCurl);//通过curl_getinfo()可以得到请求头的信息$a=curl_getinfo($oCurl);
Copy after login

Related recommendations:

php CURL gets request headers and response headers

HTTP request header

php View request header information to get the remote image size method sharing_PHP tutorial

The above is the detailed content of How to obtain CURL request headers and response headers in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template