PHP curl 获取当前请求 header 信息

本文首发于 https://blog.kangxuanpeng.com/post/php-curl_getinfo

背景

工作中经常需要对接第三方 api ,排查问题的时候需要提供请求详细参数信息。
有时候可能是认证头 Authorization 有误,如果不一并记录获取提供检查,可能会走很多弯路都不能发现问题所在。

正常 curl 获取响应结果

$url = 'xxx.kangxuanpeng.com/xx/server';
$data = ['param' => 'test'];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("content-type:application/json"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);  // 获取响应内容
curl_close($ch);

这样只能获取得到 api 响应内容 body,如果出错了或者是返回非期望内容。需要联系提供方排查问题,我们可以用 php 的 curl_getinfo 函数获取更详细的信息。

curl_getinfo() 获取响应详情


...
$response = curl_exec($ch);  // 获取响应内容
$info = curl_getinfo($ch);
$error = curl_error($ch);

curl_close($ch);

利用 curl_getinfo() 获取响应详情,可以获得以下内容结构:

 $info = Array
(
    [url] => xxx.kangxuanpeng.com/xx/server
    [content_type] => application/json;charset=UTF-8
    [http_code] => 200
    [header_size] => 196
    [request_size] => 144
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.266
    [namelookup_time] => 0.063
    [connect_time] => 0.188
    [pretransfer_time] => 0.188
    [size_upload] => 8
    [size_download] => 33
    [speed_download] => 124
    [speed_upload] => 30
    [download_content_length] => -1
    [upload_content_length] => 8
    [starttransfer_time] => 0.266
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 161.117.85.239
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 192.168.15.86
    [local_port] => 61451
)

curl_error() 可以获取 http 错误信息

获取发送的请求 header 信息

需要在 curl_exec() 之前设置

//至关重要,CURLINFO_HEADER_OUT选项可以拿到请求头信息
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);

$response = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);

curl_close($ch);

这样执行后得到的 info 如下,其中 request_header 为请求的 header 信息:

 $info = Array
(
    [url] => xxx.kangxuanpeng.com/xx/server
    [content_type] => application/json;charset=UTF-8
    [http_code] => 200
    [header_size] => 196
    [request_size] => 144
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.218
    [namelookup_time] => 0
    [connect_time] => 0.125
    [pretransfer_time] => 0.125
    [size_upload] => 8
    [size_download] => 33
    [speed_download] => 151
    [speed_upload] => 36
    [download_content_length] => -1
    [upload_content_length] => 8
    [starttransfer_time] => 0.218
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 161.117.85.239
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 192.168.15.86
    [local_port] => 61756
    [request_header] => POST /xx/server HTTP/1.1
Host: xxx.kangxuanpeng.com
Accept: */*
content-type:application/json
Content-Length: 8


)

PS: 如果 api 是根据 http 状态来区分执行结果的话,可以选择不接收响应体,可以节省带宽以及时间。

// 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文
curl_setopt($oCurl, CURLOPT_NOBODY, true);
Responses
  1. Hey there! Do you use Twitter? I'd like to follow you if that would be ok. I'm undoubtedly enjoying your blog and look forward to new posts. dating sim http://onlinedatingdd.com/

    Reply
  2. I know this if off topic but I'm looking into starting my own weblog and was wondering what all is required to get set up? I'm assuming having a blog like yours would cost a pretty penny? I'm not very web savvy so I'm not 100% positive. Any suggestions or advice would be greatly appreciated. Kudos asmr 0mniartist

    Reply
  3. Since the admin of this web site is working, no hesitation very quickly it will be well-known, due to its feature contents. asmr 0mniartist

    Reply
  4. I like the valuable information you provide to your articles. I'll bookmark your blog and test once more here regularly. I am quite sure I will be told many new stuff proper here! Best of luck for the following! 0mniartist asmr

    Reply
  5. Very nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed browsing your blog posts. In any case I'll be subscribing to your rss feed and I hope you write again soon! 0mniartist asmr

    Reply
  6. I've learn several just right stuff here. Definitely price bookmarking for revisiting. I wonder how a lot attempt you put to make this sort of wonderful informative web site. 0mniartist asmr

    Reply