<?php/* Use method $ch = new Curl_class (), $ch->set_action ("Login", $loginurl, $refer); $postdata = Array ("username" = "Fortest", "password" = "12345"), $ch->open ()->get_cookie ($this->_cookie)->get ("login", $postdata); $result = $ch->header (). $ch->body (); $ch->close (); *///curl class source code class curl_class{Private $_is_temp_cookie = false; Private $_header; Private $_body; Private $_ch; Private $_proxy; Private $_proxy_port; Private $_proxy_type = ' HTTP '; or SOCKS5 private $_proxy_auth = ' BASIC '; or NTLM private $_proxy_user; Private $_proxy_pass; protected $_cookie; protected $_options; Protected $_url = Array (); Protected $_referer = Array (); Public function __construct ($options = Array ()) {$defaults = array (); $defaults [' timeout '] = 30; $defaults [' temp_root '] = Sys_get_temp_dir (); $defaults [' user_agent '] = ' mozilla/5.0 (Windows; U Windows NT 6.0; ZH-CN; rv:1.8.1.20) gecko/20081217 FirEfox/2.0.0.20 '; $this->_options = Array_merge ($defaults, $options); Public Function open () {$this->_ch = Curl_init (); curl_setopt ($this->_ch, curlopt_followlocation, true); curl_setopt ($this->_ch, Curlopt_header, true); curl_setopt ($this->_ch, Curlopt_returntransfer, true); curl_setopt ($this->_ch, curlopt_useragent, $this->_options [' user_agent ']); curl_setopt ($this->_ch, Curlopt_connecttimeout, $this->_options [' timeout ']; curl_setopt ($this->_ch, Curlopt_httpheader, Array (' Expect: ')); For LIGHTTPD 417 expectation Failed $this->_header = '; $this->_body = "; return $this; Public function Close () {if (Is_resource ($this->_ch)) {curl_close ($this->_ch); if (Isset ($this->_cookie) && $this->_is_temp_cookie && is_file ($this->_cookie)) {Unlink ($This->_cookie); }} public Function cookie () {if (! isset ($this->_cookie)) {if (! empty ($this->_cooki e) && $this->_is_temp_cookie && is_file ($this->_cookie)) {unlink ($this->_co Okie); } $this->_cookie = Tempnam ($this->_options [' temp_root '], ' curl_manager_cookie_ '); $this->_is_temp_cookie = true; } curl_setopt ($this->_ch, Curlopt_cookiejar, $this->_cookie); Write Cookie curl_setopt ($this->_ch, Curlopt_cookiefile, $this->_cookie); Read cookies; return $this; Public Function Get_cookie ($cookfile) {curl_setopt ($this->_ch, Curlopt_returntransfer, true); curl_setopt ($this->_ch, Curlopt_cookiejar, $cookfile); return $this; Public Function Set_cookie ($cookfile) {curl_setopt ($this->_ch, Curlopt_returntransfer, true); curl_setopt ($this->_ch, CUrlopt_cookiefile, $cookfile); return $this; The Public Function set_session ($session) {//$session is True or false curl_setopt ($this->_ch, curlopt_cookiesession, $s Ession); When enabled, Curl simply passes a session cookie, ignoring other cookies, and by default, curl returns all cookies to the server. Session cookies are cookies that are used to determine if the session on the server is valid. return $this; Public Function SSL () {curl_setopt ($this->_ch, Curlopt_ssl_verifypeer, false); return $this; Public Function proxy ($host = null, $port = NULL, $type = NULL, $user = NULL, $pass = NULL, $auth = null) {$t His->_proxy = Isset ($host)? $host: $this->_proxy; $this->_proxy_port = isset ($port)? $port: $this->_proxy_port; $this->_proxy_type = isset ($type)? $type: $this->_proxy_type; $this->_proxy_auth = isset ($auth)? $auth: $this->_proxy_auth; $this->_proxy_user = isset ($user)? $user: $this->_proxy_user; $this->_proxy_pass = isset ($pass)? $pass: $this->_proxy_pass; if (! empty ($this->_proxy)) {curl_setopt ($this->_ch, curlopt_proxytype, $this->_proxy_type = = ' H TTP '? CURLPROXY_HTTP:CURLPROXY_SOCKS5); curl_setopt ($this->_ch, Curlopt_proxy, $this->_proxy); curl_setopt ($this->_ch, Curlopt_proxyport, $this->_proxy_port); } if (! empty ($this->_proxy_user)) {curl_setopt ($this->_ch, Curlopt_proxyauth, $this->_PR Oxy_auth = = ' BASIC '? CURLAUTH_BASIC:CURLAUTH_NTLM); curl_setopt ($this->_ch, Curlopt_proxyuserpwd, "[{$this->_proxy_user}]:[{$this->_proxy_pass}]"); } return $this; Public Function post ($action, $query = Array ()) {//Print_r ($query); Die if (Is_array ($query) &&!empty ($query)) {foreach ($query as $key =>& $val) { Die (' BB '); if (!empty ($val) && $val {0}!= ' @ ') {$val = UrlEncode (mb_convert_encoding ($val, ' utf-8 ', ' gb2312 ')); }} $query = Http_build_query ($query);//Echo $query ;//Die; }else{//Multiple Choice $query = $query; }//print_r ($this->_url[$action]); Die Print_r ($query); Die curl_setopt ($this->_ch, Curlopt_post, true); curl_setopt ($this->_ch, Curlopt_url, $this->_url[$action]); curl_setopt ($this->_ch, Curlopt_referer, $this->_referer[$action]); curl_setopt ($this->_ch, Curlopt_postfields, $query); $this->_requrest (); return $this; Public function Get ($action, $query = Array ()) {$url = $this->_url [$action]; if (! empty ($query)) {$url. = Strpos ($url, '? ') = = = False? '? ': ' & '; $url. = Is_array ($query)? Http_build_queRy ($query): $query; } curl_setopt ($this->_ch, Curlopt_url, $url); curl_setopt ($this->_ch, Curlopt_referer, $this->_referer [$action]); $this->_requrest (); return $this; } public Function GetInfo () {return curl_getinfo ($this->_ch); Public Function Output_trace ($output) {curl_setopt ($this->_ch, curlopt_verbose,1); curl_setopt ($this->_ch, Curlopt_stderr, $output); return $this; Public function Download ($action, $query = Array (), $saveto, $rewritemode =0) {$url = $this->_url [$action] ; if (! empty ($query)) {$url. = Strpos ($url, '? ') = = = False? '? ': ' & '; $url. = Is_array ($query)? Http_build_query ($query): $query; } try {if ($rewritemode ==1 | |!file_exists ($saveto)) {$fp = @fopen ($s Aveto, "WB"); @curl_setopt ($this->_ch, Curlopt_file,$FP); curl_setopt ($this->_ch, Curlopt_url, $url); curl_setopt ($this->_ch, curlopt_header,0); curl_setopt ($this->_ch, Curlopt_referer, $this->_referer [$action]); Curl_exec ($this->_ch); Curl_close ($this->_ch); @fclose ($FP); } return true; } catch (Exception $e) {return false; }} public function put ($action, $query = Array ()) {curl_setopt ($this->_ch, curlopt_customrequest, ' put ‘ ); return $this->post ($action, $query); Public Function Delete ($action, $query = Array ()) {curl_setopt ($this->_ch, Curlopt_customrequest, ' delet E '); return $this->post ($action, $query); Public function Head ($action, $query = Array ()) {curl_setopt ($this->_ch, Curlopt_customrequest, ' head ') ; return $this->post ($action, $query); } publiC function Options ($action, $query = Array ()) {curl_setopt ($this->_ch, curlopt_customrequest, ' options '); return $this->post ($action, $query); Public function Trace ($action, $query = Array ()) {curl_setopt ($this->_ch, curlopt_customrequest, ' Trace ' ); return $this->post ($action, $query); Public Function Connect () {} public Function follow_location () {Preg_match (' #Location: \s* (. +) #i ', $t His->header (), $match); if (Isset ($match [1])) {$this->set_action (' Auto_location_gateway ', $match [1], $this->effective_url () ); $this->get (' Auto_location_gateway ')->follow_location (); } return $this; The Public Function set_action ($action, $url, $referer = ") {$this->_url [$action] = $url; $this->_referer [$action] = $referer; return $this; } Public Function header () {return $this->_header; } public function Body () {return $this->_body; } public Function Effective_url () {return curl_getinfo ($this->_ch, Curlinfo_effective_url); } public Function Http_code () {return curl_getinfo ($this->_ch, Curlinfo_http_code); } Private Function _requrest () {$response = curl_exec ($this->_ch); $errno = Curl_errno ($this->_ch); if ($errno > 0) {throw new Curl_manager_exception (Curl_error ($this->_ch), $errno); } $header _size = Curl_getinfo ($this->_ch, curlinfo_header_size); $this->_header = substr ($response, 0, $header _size); $this->_body = substr ($response, $header _size); Public Function __destruct () {$this->close (); }}class Curl_manager_exception extends Exception {}
Handy Curl Class