Curl encapsulation
[PHP] code
Ch = curl_init (); $ this-> url = $ url? $ Url: $ this-> url; // $ this-> set_useragent =$ _ SERVER ['http _ USER_AGENT ']; // simulate your browser $ this-> set_useragent = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) version/7.0 Mobile/10B350 Safari/9537.53 "; // $ this-> set_useragent =" Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) chrome/36.0.1985.143 Safari/537.36 "; // $ this-> cook Ie_file = dirname (_ FILE __). "/cookie _". md5 (basename (_ FILE __)). ". txt "; // initialize the cookie file path // $ this-> cookie_file = SAE_TMP_PATH.TmpFS; $ this-> cookie_file =" saekv: // cookie_2014.txt ";} // close curl public function close () {curl_close ($ this-> ch);} // destructor public function _ destruct () {$ this-> close () ;}// set the timeout public function set_time_out ($ timeout = 20) {if (intval ($ timeout )! = 0) $ this-> set_time_out = $ timeout; return $ this;} // set the public function set_referer ($ referer = ") {if (! Empty ($ referer) curl_setopt ($ this-> ch, CURLOPT_REFERER, $ referer); return $ this ;} // Set cookie storage mode 1 client and 2 server files public function set_cookie_mode ($ mode = "") {$ this-> cookie_mode = $ mode; return $ this ;} // load cookie public function load_cookie () {if ($ this-> cookie_mode = 1) {if (isset ($ _ COOKIE ['curl']) {curl_setopt ($ this-> ch, CURLOPT_COOKIE, $ _ COOKIE ['curl']);} else {$ this-> exec (); curl_setopt ($ this-> ch, C URLOPT_COOKIE, $ this-> cookie_file) ;}} if ($ this-> cookie_mode = 2) {curl_setopt ($ this-> ch, CURLOPT_COOKIEFILE, $ this-> cookie_file);} if ($ this-> cookie_mode = 3) {$ kv = new SaeKV (); $ ret = $ kv-> init (); $ ret = $ kv-> get ('curl _ cooker'); if ($ ret) curl_setopt ($ this-> ch, CURLOPT_COOKIE, $ ret );} return $ this;} // Set The cookie saving mode $ cookie_val mode 1 to variable mode 2 to the file path public function save_cookie ($ cookie_val = ""){/ /Save it on the client if ($ this-> cookie_mode = 1 & $ cookie_val) {setcookie ('Curl', $ cookie_val );} // Save the server side if ($ this-> cookie_mode = 2) {if (! Empty ($ cookie_val) $ this-> cookie_file = $ cookie_val; curl_setopt ($ this-> ch, CURLOPT_COOKIEJAR, $ this-> cookie_file );} // save it in sae if ($ this-> cookie_mode = 3 & $ cookie_val) {$ kv = new SaeKV (); $ ret = $ kv-> init (); $ ret = $ kv-> get ('curl _ cookier'); if ($ ret) {$ ret = $ kv-> set ('curl _ cookies ', $ cookie_val);} else {$ ret = $ kv-> add ('curl _ cookie ', $ cookie_val);} return $ this;} // post parameter (array) $ post public Function post ($ post = "") {if ($ post & is_array ($ post) {curl_setopt ($ this-> ch, CURLOPT_POST, 1 ); curl_setopt ($ this-> ch, CURLOPT_POSTFIELDS, $ post);} return $ this;} // sets the proxy, for example, '68. 119.83.81: 27977 'public function set_proxy ($ proxy = "") {if ($ proxy) {curl_setopt ($ this-> ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5 ); curl_setopt ($ this-> ch, CURLOPT_PROXY, $ proxy);} return $ this;} // sets the public funct of the forged ip address. Ion set_ip ($ ip = "") {if (! Empty ($ ip) curl_setopt ($ this-> ch, CURLOPT_HTTPHEADER, array ("X-FORWARDED-FOR: $ ip", "CLIENT-IP: $ ip"); return $ ip ;} // Set whether to display the returned header information public function show_header ($ show = 0) {$ this-> show_header = 0; if ($ show) $ this-> show_header = 1; return $ this;} // Set the request header information public function set_useragent ($ str = "") {if ($ str) $ this-> set_useragent = $ str; return $ this;} // execute public function exec ($ url = "") {if (! $ Url) $ url = $ this-> url; curl_setopt ($ this-> ch, CURLOPT_URL, $ url); // curl_setopt ($ this-> ch, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source curl_setopt ($ this-> ch, CURLOPT_RETURNTRANSFER, 1 ); // The obtained information returns curl_setopt ($ this-> ch, CURLOPT_SSL_VERIFYHOST, 1) as a file stream ); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ this-> ch, CURLOPT_USERAGENT, $ this-> set_useragent ); // simulate the user's browser curl_setopt ($ this-> ch, CURLOPT_FOLLOW LOCATION, 1); // use automatic jump curl_setopt ($ this-> ch, CURLOPT_AUTOREFERER, 1); // automatically set Referer curl_setopt ($ this-> ch, CURLOPT_TIMEOUT, $ this-> set_time_out); // timeout setting curl_setopt ($ this-> ch, CURLOPT_HEADER, $ this-> show_header ); // display the returned Header content curl_setopt ($ this-> ch, CURLOPT_NOBODY, 0); // The response body content $ res = curl_exec ($ this-> ch) is not returned ); $ this-> flag_if_have_run = true; if (curl_errno ($ this-> ch) {// echo 'errno '. Curl_error ($ this-> ch); return false;} if ($ this-> show_header = 1) {// returns the header information and body Information list ($ header, $ body) = explode ("\ r \ n", $ res); $ arr ['header'] = $ header; $ arr ['body'] = $ body; if ($ this-> cookie_mode = 1 | $ this-> cookie_mode = 3) {preg_match_all ("/set \-cookie :( [^ \ r \ n] *)/I", $ header, $ matches); // print_r ($ matches ); if ($ matches & isset ($ matches [1]) {$ val = implode (';', array_unique (explode ('; ', Implode ('; ', $ matches [1]); // deduplicated if ($ val) $ this-> save_cookie ($ val ); // Set the client to save the cookie} if ($ arr) return $ arr;} return $ res;} // return curl_getinfo information public function get_info () {if ($ this-> flag_if_have_run = true) return curl_getinfo ($ this-> ch); else throw new Exception ("run first (execute exec ), then retrieve the information ") ;}}?>