<?php 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); } 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 number of 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; } } ?> Usage Get usage: $curl = new curl (); $curl->get (' http://www.111cn.net/'); Post usage $curl = new curl (); $curl->get (' http://www.111cn.net/', ' p=1&time=0′ '); |