Curl in PHP gets the request header and the response header

Source: Internet
Author: User
This article is mainly to share with you in PHP to get the request header and the response header, hope to help everyone.

1. Get the response header from Curl

$oCurl = Curl_init ();//Set the request header, sometimes need, sometimes do not, see if the request URL has corresponding requirements $header[] = "content-type:application/x-www-form-urlencoded" ; $user _agent = "mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1750.146 safari/537.36"; c Url_setopt ($oCurl, Curlopt_url, $SURL) curl_setopt ($oCurl, Curlopt_httpheader, $header);//Return to Response_header, This option is very important, if not true, only gets the body of the response curl_setopt ($oCurl, Curlopt_header, true);//Whether the body of the response is not required, in order to save bandwidth and time, in case only need to respond to the header can not body curl _setopt ($oCurl, Curlopt_nobody, true);//Use the uacurl_setopt defined above ($oCurl, curlopt_useragent, $user _agent); curl_setopt ($ Ocurl, Curlopt_returntransfer, 1);//no POST request, meaning to curl_setopt ($oCurl, Curlopt_post, false) via GET request; $sContent = Curl _exec ($oCurl);//Get response result in: Head size $headersize = Curl_getinfo ($oCurl, curlinfo_header_size);//To Get header information content $header = substr ($sContent, 0, $headerSize);    Curl_close ($oCurl);

2. Get the request header from Curl

 $oCurl = Curl_init (); curl_setopt ($oCurl, Curlopt_url, "https:// 117.28.240.235:8002/ipcc/agent/login "); curl_setopt ($oCurl, Curlopt_httpheader, $header);//Turn off HTTPS authentication curl_setopt ($oCurl, Curlopt_ssl_verifypeer, false); curl_setopt ($oCurl, Curlopt_ssl_verifyhost, false);//Critical, curlinfo_header_out option to get the request header information curl_setopt ($oCurl, Curlinfo_header_out, TRUE); curl_setopt ($oCurl, Curlopt_returntransfer, 1); curl_setopt ($oCurl, Curlopt_post, 1); Curl _setopt ($oCurl, Curlopt_postfields, $bodystr), $sContent = Curl_exec ($oCurl);//Curl_getinfo () can get the information of the request header $a=curl_ GetInfo ($oCurl); 

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.