Original personal address: www. phpthinking. comarchives204PHP encapsulation CURL extension PHPcurl? Php *** @ description: encapsulate CURL extension * @ date: 2014-07-2816: 04 ***** @ encoding specification * @ class name first letter capitalized, class Name: multiple words, each of which is capitalized. For example: classCurl, classCu
Personal Original address: http://www.phpthinking.com/archives/204 PHP encapsulation CURL extension PHP curl? Php/*** @ description: encapsulate CURL extension * @ date: * // *** @ encoding specification * @ class name uppercase letters, class name multiple words, the first letter of each big character is capital. For example: class Curl, class Cu
Original address: http://www.phpthinking.com/archives/204
PHP encapsulation CURL extension PHP curl
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 _;}}