- Curl Class
- Class Curl
- {
- function Curl () {
- return true;
- }
- function Execute ($method, $url, $fields = ', $userAgent = ', $httpHeaders = ', $username = ', $password = ') {
- $ch = Curl::create ();
- if (false = = = $ch) {
- return false;
- }
- if (is_string ($url) && strlen ($url)) {
- $ret = curl_setopt ($ch, Curlopt_url, $url);
- }else{
- return false;
- }
- Whether to display header information
- curl_setopt ($ch, Curlopt_header, false);
- //
- curl_setopt ($ch, Curlopt_returntransfer, true);
- if ($username! = ") {
- curl_setopt ($ch, Curlopt_userpwd, $username. ':' . $password);
- }
- $method = Strtolower ($method);
- if (' post ' = = $method) {
- curl_setopt ($ch, Curlopt_post, true);
- if (Is_array ($fields)) {
- $sets = Array ();
- foreach ($fields as $key = = $val) {
- $sets [] = $key. '=' . UrlEncode ($val);
- }
- $fields = Implode (' & ', $sets);
- }
- curl_setopt ($ch, Curlopt_postfields, $fields);
- }else if (' put ' = = $method) {
- curl_setopt ($ch, Curlopt_put, true);
- }//Bbs.it-home.org
- curl_setopt ($ch, curlopt_progress, true);
- curl_setopt ($ch, Curlopt_verbose, true);
- curl_setopt ($ch, Curlopt_mute, false);
- curl_setopt ($ch, Curlopt_timeout, 10);//Set Curl timeout seconds
- if (strlen ($userAgent)) {
- curl_setopt ($ch, curlopt_useragent, $userAgent);
- }
- if (Is_array ($httpHeaders)) {
- curl_setopt ($ch, Curlopt_httpheader, $httpHeaders);
- }
- $ret = curl_exec ($ch);
- if (Curl_errno ($ch)) {
- Curl_close ($ch);
- Return Array (Curl_error ($ch), Curl_errno ($ch));
- }else{
- Curl_close ($ch);
- if (!is_string ($ret) | |!strlen ($ret)) {
- return false;
- }
- return $ret;
- }
- }
- Function post ($url, $fields, $userAgent = ", $httpHeaders =", $username = ", $password =") {
- $ret = Curl::execute (' POST ', $url, $fields, $userAgent, $httpHeaders, $username, $password);
- if (false = = = $ret) {
- return false;
- }
- if (Is_array ($ret)) {
- return false;
- }
- return $ret;
- }
- function Get ($url, $userAgent = ', $httpHeaders = ', $username = ', $password = ') {
- $ret = Curl::execute (' GET ', $url, ', $userAgent, $httpHeaders, $username, $password);
- if (false = = = $ret) {
- return false;
- }
- if (Is_array ($ret)) {
- return false;
- }
- return $ret;
- }
- function Create () {
- $ch = null;
- if (!function_exists (' Curl_init ')) {
- return false;
- }
- $ch = Curl_init ();
- if (!is_resource ($ch)) {
- return false;
- }
- return $ch;
- }
- }
- ?>
Copy Code1,get usage:
- $curl = new curl ();
- $curl->get (' http://bbs.it-home.org/');
Copy Code2,post usage
- $curl = new curl ();
- $curl->get (' http://bbs.it-home.org/', ' p=1&time=0′ ');
Copy Code |