论坛首页 编程语言技术论坛

仅供参考, cURL 二次封装的类库 Curl_Manager

浏览 4886 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-07-03   最后修改:2010-07-09
PHP
cURL是一个可以在命令行下发起http请求的工具,phpl有调用ibcurl的适配器(adapter),所以在php里可以很方便的使用这个工具。

在使用cURL的过程中是否觉得难以使用,选项太多,没有封装,使用不够方便?

为了方便使用,我对php的cURL进行了二次封装,基于组合模式,使用灵活,能满足多数应用场景,经过我长期的实践。

  • 支持 ssl/proxy
  • 支持 cookie,会记录所有请求的cookie并带上(如需获得cookie的值,需要自己解析)
  • 支持常见请求方法get/post/put/delete 等等RESTFul所需要的方法
  • 支持文件上传
  • 支持返回http报文和报头
  • 支持带cookie的follow location(如果使用curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);cookie并不会自动带上)
  • 解决cURL请求lighttpd会产生417 Expectation Failed错误的问题

实际上Curl_Manager并没有开发到心目中理想的样子,但并不妨碍使用,仅供各位参考:)

测试例子:
<?php
require_once("Curl_Manager.php");

$manager = new Curl_Manager();

$manager->set_action('gg', 'http://www.google.com/', 'http://willko.iteye.com/');//设置动作,(动作名称, 动作对应url,来源referer)

$manager->open()->get('gg'); //打开一个请求,进行get操作

echo $manager->body(); // 获得报文
echo $manager->header(); // 获得报头(需要自己解析)

//发起ssl请求
$manager->set_action('taobao', 'https://login.taobao.com/member/login.jhtml?f=top&redirectURL=http://www.taobao.com/', 'http://willko.iteye.com/');

$manager->ssl()->get('taobao'); //使用ssl方法,让当前这个请求支持ssl请求

echo $manager->body();
echo $manager->header();


支持cookie:
$manager->cookie();

带参数请求,get/post/put/delete等等使用方式一样
$manager->post('action', array('k' => 'v', 'a' => 'b'));
$manager->post('action', 'k=v&a=b');
$manager->post('action', array('k' => 'v', '@a' => '/home/www/avatar.gif')); //上传,需要使用绝对路径,参数名称加"@"。


Curl_Manager代码,也可以从附件下载:
<?php
// +----------------------------------------------------------------------+
// | Copyright (c) 2008-2010 Willko Cheng                                 |
// +----------------------------------------------------------------------+
// | Authors: Willko Cheng <willko@foxmail.com>                           |
// | Blog: http://willko.iteye.com/                                     |
// +----------------------------------------------------------------------+

class Curl_Manager {
	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->_cookie ) && $this->_is_temp_cookie && is_file ( $this->_cookie )) {
				unlink ( $this->_cookie );
			}
			
			$this->_cookie = tempnam ( $this->_options ['temp_root'], 'curl_manager_cookie_' );
			$this->_is_temp_cookie = true;
		}
		
		curl_setopt ( $this->_ch, CURLOPT_COOKIEJAR, $this->_cookie );
		curl_setopt ( $this->_ch, CURLOPT_COOKIEFILE, $this->_cookie );
		
		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) {
		$this->_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 == 'HTTP' ? 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->_proxy_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()) {
		if (is_array($query)) {
			foreach ($query as $key => $val) {
				if ($val{0} != '@') {
					$encode_key = urlencode($key);

					if ($encode_key != $key) {
						unset($query[$key]);
					}

					$query[$encode_key] = urlencode($val);
				}
			}
		}
		
		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 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, 'DELETE' );
		
		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', $this->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;
	}
	
	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 {

}

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics