<?php
/**
* CURL HTTP ,
* :
* 1: ssl proxy
* 2: cookie
* 3: GET/POST
* 4: , .
* 5: Header
* 6: lighttpd
* 7: REFERER
* 8: 301 ( GG)
* 9: , , ,USERAGENT,Gzip .
============= ===============
include('clsss/class.curl.php');
$cu = new Curl();
// baidu
echo $cu->get('http://www.baidu.com');
// QuickPHP
$cu->post('http://www.myquickphp.com/code/qpdemo/?c=main&a=login',array('uname'=>'admin','upass'=>'admin'));
echo $cu->get('http://www.myquickphp.com/code/qpdemo');
// http://a.com/a.php , a.php :
//<?php print_r($_POST);print_r($_FILES); />
echo $cu->post('http://a.com/a.php',array('id'=>1,'name'=>'yuanwei'),
array('img'=>'file/a.jpg','files'=>array('file/1.zip','file/2.zip')));
//
echo '<br/>','ERRNO=',$cu->errno();
echo '<br/>','ERROR=',$cu->error();
echo '<pre>'.print_r($cu->getinfo(),true).'</pre>';
*/
class Curl{
//CURL
private $ch = null;
//CURL
private $info = array();
//CURL SETOPT
private $setopt = array(
// ,http 80
'port'=>80,
// USERAGENT, :"Mozilla/4.0",
'userAgent'=>'',
//
'timeOut'=>30,
// COOKIE ,
'useCookie'=>true,
// SSL
'ssl'=>false,
// gzip
'gzip'=>true,
//
'proxy'=>false,
// , HTTP SOCKS5
'proxyType'=>'HTTP',
// , HTTP URL :"http://www.proxy.com"
//SOCKS5 IP , :"192.168.1.1"
'proxyHost'=>'http://www.proxy.com',
//
'proxyPort'=>1234,
// (HTTP )
'proxyAuth'=>false,
// . BASIC NTLM
'proxyAuthType'=>'BASIC',
//
'proxyAuthUser'=>'user',
'proxyAuthPwd'=>'password',
);
/**
*
*
* @param array $setopt : private $setopt
*/
public function __construct($setopt=array())
{
//
$this->setopt = array_merge($this->setopt,$setopt);
// CURL
function_exists('curl_init') || die('CURL Library Not Loaded');
//
$this->ch = curl_init();
// CURL
curl_setopt($this->ch, CURLOPT_PORT, $this->setopt['port']);
//
if($this->setopt['proxy']){
$proxyType = $this->setopt['proxyType']=='HTTP' ? CURLPROXY_HTTP : CURLPROXY_SOCKS5;
curl_setopt($this->ch, CURLOPT_PROXYTYPE, $proxyType);
curl_setopt($this->ch, CURLOPT_PROXY, $this->setopt['proxyHost']);
curl_setopt($this->ch, CURLOPT_PROXYPORT, $this->setopt['proxyPort']);
//
if($this->setopt['proxyAuth']){
$proxyAuthType = $this->setopt['proxyAuthType']=='BASIC' ? CURLAUTH_BASIC : CURLAUTH_NTLM;
curl_setopt($this->ch, CURLOPT_PROXYAUTH, $proxyAuthType);
$user = "[{$this->setopt['proxyAuthUser']}]:[{$this->setopt['proxyAuthPwd']}]";
curl_setopt($this->ch, CURLOPT_PROXYUSERPWD, $user);
}
}
// “Location:” header
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
// SSL
if($this->setopt['ssl']){
//
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
// SSL
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, true);
}
// http , lighttpd
$header[]= 'Expect:';
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);
// HTTP USERAGENT
$userAgent = $this->setopt['userAgent'] ? $this->setopt['userAgent'] : $_SERVER['HTTP_USER_AGENT'];
curl_setopt($this->ch, CURLOPT_USERAGENT, $userAgent);
// ,0
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->setopt['timeOut']);
// curl
curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->setopt['timeOut']);
// gzip
if($this->setopt['gzip']){
curl_setopt($this->ch, CURLOPT_ENCODING, 'gzip');
}
// COOKIE
if($this->setopt['useCookie']){
// COOKIE ( )
$cookfile = tempnam('/tmp','cuk');
//pr(sys_get_temp_dir());
// , cookie
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookfile);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookfile);
}
// (HEADER ),
curl_setopt($this->ch, CURLOPT_HEADER, true);
// , 。
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, true) ;
}
/**
* GET
*
* @param string $url : URL
* @param array $params : , : array('id'=>10,'name'=>'yuanwei')
* @param array $referer : , , .
* @return :false :
*/
public function get($url,$params=array(), $referer='')
{
return $this->_request('GET', $url, $params, array(), $referer);
}
/**
* POST
*
* @param string $url : URL
* @param array $params : , : array('id'=>10,'name'=>'yuanwei')
* @param array $uploadFile : , ,
* :array('img1'=>'./file/a.jpg')
* :array('img'=>array('./file/a.jpg','./file/b.jpg'))
* @param array $referer : , , , .
* @return :false :
*/
public function post($url,$params=array(),$uploadFile=array(), $referer='')
{
return $this->_request('POST', $url, $params, $uploadFile, $referer);
}
/**
*
*
* @return string
*/
public function error()
{
return curl_error($this->ch);
}
/**
*
*
* @return int
*/
public function errno()
{
return curl_errno($this->ch);
}
/**
* Header ,
* [before] :
* [after] :
* [header] : Header
*
* @return array
*/
public function getInfo()
{
return $this->info;
}
/**
*
*
*/
public function __destruct()
{
// CURL
curl_close($this->ch);
}
/**
* :
*
* @param string $method :HTTP
* @param string $url : URL
* @param array $params :
* @param array $uploadFile : ( POST )
* @param array $referer :
* @return :false :
*/
private function _request($method, $url, $params=array(), $uploadFile=array(), $referer='')
{
// GET URL
if($method == 'GET'){
$url = $this->_parseUrl($url,$params);
}
// URL
curl_setopt($this->ch, CURLOPT_URL, $url);
// POST
if($method == 'POST'){
// POST , :application/x-www-form-urlencoded
curl_setopt($this->ch, CURLOPT_POST, true) ;
// POST
$postData = $this->_parsmEncode($params,false);
//
if($uploadFile){
foreach($uploadFile as $key=>$file){
if(is_array($file)){
$n = 0;
foreach($file as $f){
//
$postData[$key.'['.$n++.']'] = '@'.realpath($f);
}
}else{
$postData[$key] = '@'.realpath($file);
}
}
}
//pr($postData); die;
//
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postData);
}
// ,
if($referer){
curl_setopt($this->ch, CURLOPT_REFERER, $referer);
}else{
curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
}
//
$this->info['before'] = curl_getinfo($this->ch);
//
$result = curl_exec($this->ch);
//
$headerSize = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
$this->info['header'] = substr($result, 0, $headerSize);
//
$result = substr($result, $headerSize);
//
$this->info['after'] = curl_getinfo($this->ch);
//
if($this->errno() == 0){ //&& $this->info['after']['http_code'] == 200
return $result;
}else{
return false;
}
}
/**
* URL,GET
*
* @param string $url :URL
* @param array $params : URL
* @return string
*/
private function _parseUrl($url,$params)
{
$fieldStr = $this->_parsmEncode($params);
if($fieldStr){
$url .= strstr($url,'?')===false ? '?' : '&';
$url .= $fieldStr;
}
return $url;
}
/**
* ENCODE
*
* @param array $params :
* @param bool $isRetStr : true: false:
* @return string || array
*/
private function _parsmEncode($params,$isRetStr=true)
{
$fieldStr = '';
$spr = '';
$result = array();
foreach($params as $key=>$value){
$value = urlencode($value);
$fieldStr .= $spr.$key .'='. $value;
$spr = '&';
$result[$key] = $value;
}
return $isRetStr ? $fieldStr : $result;
}
}