PHP encapsulation CURL extension instance

Source: Internet
Author: User
This article mainly introduces the php curl extension encapsulation class. The example analyzes the skills related to sending post, get request and cookie operations based on curl, which has some reference value, for more information about PHP encapsulation CURL extension, see the example in this article. Share it with you for your reference. The details are as follows:

 Ch _ = curl_init ($ url); curl_setopt ($ this-> ch _, CURLOPT_HEADER, 1); curl_setopt ($ this-> ch _, CURLOPT_RETURNTRANSFER, 1 );} /*** @ return response header */public function ResponseHeader ($ url) {if (! Function_exists ('http _ parse_headers') {function http_parse_headers ($ raw_headers) {$ headers = array (); foreach (explode ("\ n", $ raw_headers) as $ I => $ h) {$ h = explode (':', $ h, 2); if (isset ($ h [1]) {if (! Isset ($ headers [$ h [0]) {$ headers [$ h [0] = trim ($ h [1]);} else if (is_array ($ headers [$ h [0]) {$ tmp = array_merge ($ headers [$ h [0], array (trim ($ h [1]); $ headers [$ h [0] = $ tmp ;} else {$ tmp = array_merge (array ($ headers [$ h [0]), array (trim ($ h [1]); $ headers [$ h [0] = $ tmp ;}}return $ headers ;}$ this-> Start ($ url ); curl_setopt ($ this-> ch _, CURLOPT_TIMEOUT, Curl: time _); $ this-> body _ = $ this-> Execx (); $ header_size = Curl_getinfo ($ this-> ch _, CURLINFO_HEADER_SIZE); $ this-> response_header _ = substr ($ this-> body _, $ start = 0, $ offset = $ header_size ); $ this-> response_header _ = http_parse_headers ($ this-> response_header _); print_r ($ this-> response_header _); return $ this-> Close ($ this-> body _);}/*** @ read cookie */public function LoadCookie ($ url, $ cookie_file) {$ this-> Start ($ url); curl_setopt ($ this-> ch _, CURLOPT_COOKIE, 1); curl_set Opt ($ this-> ch _, CURLOPT_COOKIEFILE, $ cookie_file); $ this-> body _ = $ this-> Execx (); return $ this-> Close ($ this-> body _);}/*** @ write cookie */public function SaveCookie ($ url) {$ this-> Start ($ url); curl_setopt ($ this-> ch _, CURLOPT_COOKIE, 1); curl_setopt ($ this-> ch _, CURLOPT_COOKIEFILE, 'cookie.txt '); curl_setopt ($ this-> ch _, CURLOPT_COOKIEJAR, 'cookie.txt '); $ this-> body _ = $ this-> Execx (); return $ this-> Close ($ this-> body _);}/*** @ Set HEADER */public function SetHeader ($ headers = null) {if (is_array ($ headers) & count ($ headers)> 0) {curl_setopt ($ this-> ch _, CURLOPT_HTTPHEADER, $ headers) ;}/ *** @ GET request */public function Get ($ url, array $ params = array () {if ($ params) {if (strpos ($ url ,'? ') {$ Url. = "&". http_build_query ($ params);} else {$ url. = "? ". Http_build_query ($ params) ;}$ this-> Start ($ url); curl_setopt ($ this-> ch _, CURLOPT_TIMEOUT, Curl: time _); if (strpos ($ url, 'https') = 0) {curl_setopt ($ this-> ch _, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ this-> ch _, CURLOPT_SSL_VERIFYPEER, 0);} $ this-> body _ = $ this-> Execx (); return $ this-> Close ($ this-> body _);} /*** @ POST request */public function Post ($ url, array $ params = array () {$ this-> Start ($ url ); curl_setopt ($ this-> ch _, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt ($ this-> ch _, CURLOPT_HTTPHEADER, array ("Content-Type: application/x-www-form-urlencoded "); curl_setopt ($ this-> ch _, CURLOPT_POST, true); curl_setopt ($ this-> ch _, CURLOPT_TIMEOUT, Curl:: time _); if ($ params) {curl_setopt ($ this-> ch _, CURLOPT_POSTFIELDS, http_build_query ($ params ));} $ this-> body _ = $ this-> Execx (); return $ this-> Close ($ this-> body _);}/*** @ tips: google http head method */public function Head ($ url, array $ params = array () {$ this-> Start ($ url ); curl_setopt ($ this-> ch _, CURLOPT_TIMEOUT, Curl: time _); curl_setopt ($ this-> ch _, CURLOPT_RETURNTRANSFER, 0 ); curl_setOpt ($ this-> ch _, CURLOPT_NOBODY, true); $ this-> body _ = $ this-> Execx (); return $ this-> Close ($ this-> body _);}/*** @ execute CURL session */public function Execx () {return curl_exec ($ this-> ch _);}/*** @ Close CURL handle */public function Close ($ body _) {if ($ body _ = false) {echo "CURL Error :". curl_error ($ body _); return false;} curl_close ($ this-> ch _); return $ body _;}}

I hope this article will help you with php programming.

For more articles related to PHP encapsulation CURL extension instances, refer to PHP Chinese network!

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.