Phpcurl separates header and body Information

Source: Internet
Author: User
In php, you can use curl to simulate http requests. you can also get httpresponseheader and body. of course, you can also set parameters to retrieve only one of them. when you set the parameters, you can also get respon.

In php, you can use curl to simulate http requests and obtain the http response header and body. of course, you can also set parameters to retrieve only one of them, when the setting obtains both the response header and body, they are returned as results together. in this case, we need to separate them ourselves.

The following code simulates an http GET request to google:

  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); // indicates the 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) = '20140901 '){
  13. Return $ result;
  14. }
  15. Return NULL;
  16. }

After calling the above method, the output is similar to the following:

HTTP/1.1 200 OKDate: Tue, 09 Jul 2013 14:21:08 GMTExpires:-1Cache-Control: private, max-age = 0Content-Type: text/html; charset = UTF-8Set-Cookie: PREF = ID = 75e996a7ad21f47b: FF = 0: NW = 1: TM = 1373379668: LM = 1373379668: S = TTLQQN-rjgdynkky; expires = Thu, 09-Jul-2015 14:21:08 GMT; path =/; domain = .google.com. hkSet-Cookie: NID = 67 = PPu7FfFeuZqwfsrUifgzjidX4JZxxCPLe9xFHjdXhfHpzs3gaykFSH5uGXy2esWTlp_rdqIYkjFDMollzI_sA-8 OwxD3mDh6KCRwdMa9-g5VChj0E5XAGNjo9d-sZfLN; expires = Wed, 08-Jan-2014 14:21:08 GMT; path =/; domain = .google.com.hk; HttpOnlyP3P: CP = "This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py? Hl = en & amp; answer = 151657 for more info. "Server: gwsX-XSS-Protection: 1; mode = blockX-Frame-Options: SAMEORIGINTransfer-Encoding: chunked GoogleScript (function () {window. google = {kEI: "vbzcudwuhomti1_64ihocw", getEI: function (a) {for (var B; &&(! A. getAttribute |! (B = a. getAttribute ("eid ")));......

Here we can see that the header and body information are in the same result. There are two ways to separate them. one is to get the length of the header through the curl_getinfo () method that comes with curl, use substr to split the string. the sample code is as follows:

  1. $ Response = curl_exec ($ ch );
  2. If (curl_getinfo ($ ch, CURLINFO_HTTP_CODE) = '20140901 '){
  3. $ HeaderSize = curl_getinfo ($ ch, CURLINFO_HEADER_SIZE );
  4. $ Header = substr ($ response, 0, $ headerSize );
  5. $ Body = substr ($ response, $ headerSize );
  6. }

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

  1. $ Response = curl_exec ($ ch );
  2. If (curl_getinfo ($ ch, CURLINFO_HTTP_CODE) = '20140901 '){
  3. List ($ header, $ body) = explode ("rnrn", response, 2 );
  4. }

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.