jericho0702 发表于 2018-12-18 07:12:21

PHP--获取响应头(Response Header)方法

  方法一:
  ==========================================
  $baiduUrl = "http://www.baidu.com/link?url=LZE_J6a1AcieLlTzNxUZQVpe2trQ99zx1ls85ux8dXaGlFB3eiEm_Y6SJC1sNQf_";
  file_get_contents($baiduUrl);
  $responseInfo = $http_response_header;
  print_r($responseInfo);
  // 输出:
  Array
  (
   => HTTP/1.1 302 Found
   => Date: Fri, 27 Jun 2014 02:47:35 GMT
   => Server: Apache
   => Location: http://www.edeng.cn/s/chuna/
   => Cache-Control: max-age=86400
   => Expires: Sat, 28 Jun 2014 02:47:35 GMT
   => Content-Length: 212
   => Connection: Close
   => Content-Type: text/html;
   => HTTP/1.1 200 OK
   => Server: nginx/1.4.3
   => Date: Fri, 27 Jun 2014 02:47:35 GMT
   => Content-Type: text/html; charset=utf-8
   => Connection: close
   => Expires: Mon, 26 Jul 1997 05:00:00 GMT
   => Last-Modified: Fri, 27 Jun 2014 02:47:35 GMT
   => Cache-Control: no-store, no-cache, must-revalidate
   => Pragma: no-cache
   => Vary: User-Agent,Accept-Encoding
   => X-Cache: MISS from web1.edeng.cn
   => Via: 1.1 web1.edeng.cn:80 (squid)
  )
  遍历该数组即可得到相应的值。比如要想获得 Location 的值:
  foreach ($responseInfo as $loop) {
  if(strpos($loop, "Location") !== false){
  $edengUrl = trim(substr($loop, 10));
  print_r($edengUrl);
  // 输出: http://www.edeng.cn/s/chuna/
  }
  }
  方法二:
  ==========================================
  function get_head($sUrl){
  $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);
  // 使用上面定义的 ua
  curl_setopt($oCurl, CURLOPT_USERAGENT,$user_agent);
  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);
  curl_close($oCurl);
  return $header;
  }
  如上面解析,我们可以成功获得到头信息:
  HTTP/1.1 302 Found
  Date: Fri, 27 Jun 2014 02:47:35 GMT
  Server: Apache
  Location: http://www.edeng.cn/s/chuna/
  Cache-Control: max-age=86400
  Expires: Sat, 28 Jun 2014 02:47:35 GMT
  Connection: Keep-Alive
  Content-Type: text/html; charset=iso-8859-1
  这时候,如果我们想获得 Location项的内容,可以先把上面头正文件按回车换行切割成数组,然后再遍历匹配,如:
  $responseHead = post_head($baiduUrl);
  $headArr = explode("\r\n", $responseHead);
  foreach ($headArr as $loop) {
  if(strpos($loop, "Location") !== false){
  $edengUrl = trim(substr($loop, 10));
  print_r($edengUrl);
  // 输出: http://www.edeng.cn/s/chuna/
  }
  }

页: [1]
查看完整版本: PHP--获取响应头(Response Header)方法