网站地图    收藏   

主页 > php专栏 > php应用 >

php curl 分离header和body信息 - php高级应用

来源:自学PHP网    时间:2014-11-27 22:16 作者: 阅读:

[导读] php中可以通过curl来模拟http请求,同时可以获取http response header和body,当然也设置参数可以只获取其中的某一个,当设置同时获取respon...

php curl 分离header和body信息

php中可以通过curl来模拟http请求,同时可以获取http response header和body,当然也设置参数可以只获取其中的某一个,当设置同时获取response header和body时候,它们会一同作为结果返回,这时需要我们自己来分离它们。

下面代码是模拟向google一个http GET请求,代码如下:

  1. function httpGet() { 
  2.     $url = 'http://www.google.com.hk'
  3.     $ch = curl_init(); 
  4.     curl_setopt($ch, CURLOPT_URL, $url); 
  5.     curl_setopt($ch, CURLOPT_HEADER, TRUE);    //表示需要response header 
  6.     curl_setopt($ch, CURLOPT_NOBODY, FALSE); //表示需要response body 
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
  8.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); 
  9.     curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); 
  10.     curl_setopt($ch, CURLOPT_TIMEOUT, 120); 
  11.     $result = curl_exec($ch); 
  12.     if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') { 
  13.         return $result
  14.     } 
  15.     return NULL; 

调用上述方法后看到如下类似输出:

HTTP/1.1 200 OK
Date: Tue, 09 Jul 2013 14:21:08 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=75e996a7ad21f47b:FF=0:NW=1:TM=1373379668:LM=1373379668:S=TTLQQN-jwGDYnkkY; expires=Thu, 09-Jul-2015 14:21:08 GMT; path=/; domain=.google.com.hk
Set-Cookie: NID=67=PPu7FfFeuZqwfsrUifgzjidX4JZxxCPLe9xFHjdXhfHpzs3gaykFSH5uGXy2esWTlp_rdqIYkjFDMollzI_sA-8owxD3mDh6KCRwdMa9-g5VChj0E5XAGNjo9d-sZfLN; expires=Wed, 08-Jan-2014 14:21:08 GMT; path=/; domain=.google.com.hk; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
<!doctype html><html itemscope="itemscope" itemtype="http://schema.org/WebPage"><head><meta itemprop="image" content="/images/google_favicon_128.png"><title>Google</title><script>(function(){
window.google={kEI:"VBzcUdWuHOmtiQf64IHoCw",getEI:function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));
……

这里可以看到结果中header和body信息是在一起的,那么如何分离它们呢,方法有二种,一是通过curl自带的curl_getinfo()方法获取头的长度,然后使用substr来分割字符串,示例代码如下:

  1. $response = curl_exec($ch); 
  2. if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') { 
  3.     $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
  4.     $header = substr($response, 0, $headerSize); 
  5.     $body = substr($response$headerSize); 

第二种方法基于header和body是通过两个回车换行来分割的,所以可以通过如下代码实现:

  1. $response = curl_exec($ch); 
  2. if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') { 
  3.     list($header$body) = explode("rnrn", response, 2); 
  4. }

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论