PHP Curl separates header and body information

Source: Internet
Author: User

PHP can be used to simulate HTTP requests via curl, as well as get HTTP response headers and body, and of course set parameters to get only one of them. When the settings get both the response header and the body, they are returned together as a result. Then we need to separate them ourselves.

The following code is impersonating an HTTP GET request to Google

functionHttpGet() {$url= ' HTTP://WWW.GOOGLE.COM.HK ';$ch=Curl_init();curl_setopt($ch,Curlopt_url,$url);curl_setopt($ch,Curlopt_header,TRUE); Indicates the need to response headercurl_setopt($ch,Curlopt_nobody,FALSE); Indicates the need for response bodycurl_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) Span class= "PLN" > ==  ' ")  { return $result  } return Null}             /span>                

When you call the above method, you see an output like this:

HTTP/1.1 200OkDate: Tue, 09 Jul 2013 14:21st:08GmtExpires: -1Cache-Control: Private,Max-Age=0Content-Type:Text/Html;CharSet=UTF-8Set-Cookies:PREF=Id=75e996a7ad21f47b:Ff=0:NW=1:Tm=1373379668:Lm=1373379668:S=Ttlqqn-Jwgdynkky;Expires=Thu, 09-Jul-2015 14:21st:08Gmt;Path=/;Domain=.Google.Com.hkSet-Cookies:NID=67=Ppu7fffeuzqwfsruifgzjidx4jzxxcple9xfhjdxhfhpzs3gaykfsh5ugxy2eswtlp_rdqiykjfdmollzi_sa-8owxd3mdh6kcrwdma9-g5vchj0e5xagnjo9d-Szfln;Expires=Wed, 08-Jan-2014 14:21st:08Gmt;Path=/;Domain=.Google.Com.hk; HttpOnlyP3p:Cp="This was not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info. "Server:Gwsx-Xss-Protection: 1;Mode=Blockx-Frame-Options:SameoriginTransfer-Encoding:Chunked<!DOCTYPE HTML>Google(function(){Windowgoogle={kei: " VBZCUDWUHOMTIQF64IHOCW ",getei:function  (afor ( var B;&& (! A. Getattribute| |! (b=a. Getattribute "Eid"  ...                

Here you can see that the header and body information are together in the results, so how to separate them. There are two ways to get the length of the header by using the Curl_getinfo () method of curl, and then use SUBSTR to split the string. The sample code is as follows:

$response=Curl_exec($ch);If (Curl_getinfo($ch,Curlinfo_http_code) ==  ' ' )  {= Curl_getinfo ( $ch , Curlinfo_header_size $header = Substr ( $response  0, $headerSize  $body = Substr ( $response Span class= "pun" >, $headerSize                /span>                 

The second method is based on the header and body is divided by two carriage return line, so can be implemented by the following code:

$response=Curl_exec($ch);if  (curl_getinfo $ch , Curlinfo_http_code)  ==  ' $ '  { List ( $header , $body )  = explode  ( "\r\n\r\n" , Response ,  2               /span>                

Php Curl Separate header and body information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.