您好,欢迎访问一九零五行业门户网

php curl 分离header和body信息_PHP教程

本文章来给大家详细介绍关于在php curl 分离header和body信息测试实例,以前可能大家没注意,后来分析了一下,下面给大家介绍。
php中可以通过curl来模拟http请求,同时可以获取http response header和body,当然也设置参数可以只获取其中的某一个。当设置同时获取response header和body时候,它们会一同作为结果返回。这时需要我们自己来分离它们。
下面代码是模拟向google一个http get请求
 代码如下 复制代码
function httpget() {
    $url = 'http://www.google.com.hk';
    $ch = curl_init();
    curl_setopt($ch, curlopt_url, $url);
    curl_setopt($ch, curlopt_header, true);    //表示需要response header
    curl_setopt($ch, curlopt_nobody, false); //表示需要response body
    curl_setopt($ch, curlopt_returntransfer, true);
    curl_setopt($ch, curlopt_followlocation, false);
    curl_setopt($ch, curlopt_autoreferer, true);
    curl_setopt($ch, curlopt_timeout, 120);
    $result = curl_exec($ch);
    if (curl_getinfo($ch, curlinfo_http_code) == '200') {
        return $result;
    }
    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
google
其它类似信息

推荐信息