Home  >  Article  >  Backend Development  >  php curl separates header and body information_PHP tutorial

php curl separates header and body information_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:272084browse

This article will give you a detailed introduction to the test example of separating header and body information in php curl. You may not have noticed it before, but I analyzed it later and will introduce it to you below.

In PHP, curl can be used to simulate http requests, and at the same time, http response header and body can be obtained. Of course, parameters can also be set to obtain only one of them. When the setting obtains the response header and body at the same time, they will be returned together as the result. At this time we need to separate them ourselves.

The following code simulates an http GET request to Google

The code is as follows Copy code
 代码如下 复制代码

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;
}

function httpGet() {

$url = 'http://www.google.com.hk';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE); //Indicates the need for response header
curl_setopt($ch, CURLOPT_NOBODY, FALSE); //Indicates the need for 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;
}

 代码如下 复制代码

$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);
}

After calling the above method, you will see output similar to the following: 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