douxue4242 2015-07-06 15:23
浏览 150

有没有一个简单的解决方案来使用PHP在CURL响应中获取特定的自定义标头? [重复]

This question already has an answer here:

When I fetch all headers via

$response = curl_exec($curl);  
$header_size = curl_getinfo($curl,CURLINFO_HEADER_SIZE);
$result = substr($response, 0, $header_size);

I get an output like

HTTP/1.1 302 Found
Server: nginx
Date: Mon, 06 Jul 2015 14:58:06 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 0
Connection: keep-alive
Status: 302 Found
Location: https://data.myserver.com 
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: 9e2a562ec3ba73a72702e84fa7177c56
X-UA-Compatible: IE=Edge,chrome=1
Cache-Control: no-cache
X-Runtime: 0.046013
X-Rack-Cache: miss
Accept-Ranges: bytes
X-Varnish: 945594494
Age: 0
Via: 1.1 varnish
Strict-Transport-Security: max-age=31536000

I only want the 'Location:' value, in Java it was easy however in PHP, I could not find a simple elegant solution without using regex.

</div>
  • 写回答

1条回答 默认 最新

  • douci2015 2015-07-06 16:39
    关注

    Maybe it will help you:

    <?php
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://php.net/manual/en/function.curl-getinfo.php');
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    var_export($info);
    

    Result:

    array (
      'url' => 'http://php.net/manual/en/function.curl-getinfo.php',
      'content_type' => 'text/html; charset=utf-8',
      'http_code' => 200,
      'header_size' => 596,
      'request_size' => 81,
      'filetime' => -1,
      'ssl_verify_result' => 0,
      'redirect_count' => 0,
      'total_time' => 0.79269400000000001,
      'namelookup_time' => 0.028351999999999999,
      'connect_time' => 0.21638999999999997,
      'pretransfer_time' => 0.21643999999999997,
      'size_upload' => 0,
      'size_download' => 62628,
      'speed_download' => 79006,
      'speed_upload' => 0,
      'download_content_length' => -1,
      'upload_content_length' => 0,
      'starttransfer_time' => 0.41433799999999998,
      'redirect_time' => 0,
      'redirect_url' => '',
      'primary_ip' => '72.52.91.14',
      'certinfo' => 
      array (
      ),
      'primary_port' => 80,
      'local_ip' => '192.168.12.184',
      'local_port' => 36041,
    )
    

    And you can get http://php.net/manual/en/function.curl-getinfo.php through $info['url'].

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看