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;
- }
调用上述方法后看到如下类似输出:
这里可以看到结果中header和body信息是在一起的,那么如何分离它们呢,方法有二种,一是通过curl自带的curl_getinfo()方法获取头的长度,然后使用substr来分割字符串,示例代码如下:
- $response = curl_exec($ch);
- if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
- $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
- $header = substr($response, 0, $headerSize);
- $body = substr($response, $headerSize);
- }
第二种方法基于header和body是通过两个回车换行来分割的,所以可以通过如下代码实现:
- $response = curl_exec($ch);
- if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
- list($header, $body) = explode("rnrn", response, 2);
- }
新闻热点
疑难解答