PHP curl封装类(包含读取/写入/读写cookie/post/代理/伪造来源IP)
下面小编整理的这个PHP curl封装类可以实现的功能有含读取/写入/读写cookie,构造post参数,伪造来源IP,设置代理功能,下面来看看
PHP的curl能够实现许多非常强大的http操作,不过curl原生的写法有些蛋疼,于是自己封装了一个类。
本段代码部分代码来源网络,我自己添加了注释和一些小修改,目前实现的功能有:
构造post参数
读取/写入/读写cookie
伪造来源IP
设置代理
代码会不定时更新。
<?php /** *File:curl.class.php *Createdon:2014-8-26,8:34:01 *copyright小皓(C)2013-2099版权所有 *www.haosblog.com * *CURL封装类,本类大部分操作均支持链式操作 * *example: * *$curl=newCurl($url); *$curl->exec(); *发送post数据 *$curl->post(array('username'=>'用户名'))->exec(); */ class Curl { private $ch; private $flag_if_have_run = false; private $has_cloase = true; public function __construct($url = '', $forgeIP = false) { $this->init($url, $forgeIP); } /** *初始化CURL。如果CURL未被关闭,则先关闭 * *@paramtype$url *@return \Common\Library\Curl */ public function init($url = '', $forgeIP = false) { if (!$this->has_cloase) { //如果上一次连接尚未结束,则先关闭 $this->close(); } if ($forgeIP) { //伪造IP,将IP伪造为访问者的IP if (Validate::isIPAddress($forgeIP)) { $ip = $forgeIP; } else { $ip = $_SERVER['SERVER_ADDR']; } $this->set_ip($ip); } $this->ch = curl_init($url); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); $this->has_cloase = false; return $this; } public function setUrl($url) { curl_setopt($this->ch, CURLOPT_URL, $url); return $this; } public function close() { if (!$this->has_close) { curl_close($this->ch); $this->has_cloase = true; } } public function __destruct() { $this->close(); } /** *设置页面超时时间,支持链式操作 * *@paramtype$timeout *@return \Common\Library\Curl */ public function set_time_out($timeout) { curl_setopt($this->ch, CURLOPT_TIMEOUT, intval($timeout)); return $this; } /** *伪造来源路径 * *@paramtype$referer *@return \Common\Library\Curl */ public function set_referer($referer) { if (!empty($referer)) { curl_setopt($this->ch, CURLOPT_REFERER, $referer); } return $this; } /** *设置cookie *本方法仅发送cookie信息到远端,不保存远端返回的cookie信息 * *@paramtype$cookie_file cookie文件的存储路径 *@return \Common\Library\Curl */ public function load_cookie($cookie_file) { $this->_checkCookie($cookie_file); curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_file); return $this; } /** *设置cookie *发送cookie到远端,并保存远端返回的cookie * *@paramtype$cookie_file *@return \Common\Library\Curl */ public function cookie($cookie_file) { $this->_checkCookie($cookie_file); curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookie_file); return $this; } /** *设置cookie *本方法将不发送cookie信息,仅接收返回的cookie并保存 * *@paramtype$cookie_file *@return \Common\Library\Curl */ public function save_cookie($cookie_file = "") { //设置缓存文件,例如a.txt if (empty($cookie_file)) { $cookie_file = tempnam('./', 'cookie'); } $this->_checkCookie($cookie_file); curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookie_file); return $this; } private function _checkCookie($cookie_file) { if (!ThinkStorage::has($cookie_file)) { ThinkStorage::put($cookie_file, ''); } } /** *执行curl请求 * *@return type */ public function exec() { $str = curl_exec($this->ch); $this->flag_if_have_run = true; return $str; } /** *设置发送POST请求。没有调用过该方法默认使用GET方法提交 * *@paramtype$postData post的数据,支持数组和“xxx=1&x=2”两种格式 *@return \Common\Library\Curl */ public function post($postData) { curl_setopt($this->ch, CURLOPT_POST, 1); //echo($postQuery);die; curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postData); return $this; } /** *获取curl的信息 * *@return type *@throwsException */ public function get_info() { if ($this->flag_if_have_run == true) { return curl_getinfo($this->ch); } else { throw newException("<h1>需先运行(执行exec),再获取信息</h1>"); } } /** *设置代理 * *@paramtype$proxy *@return \Common\Library\Curl */ public function set_proxy($proxy) { //设置代理,例如'68.119.83.81:27977' curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); curl_setopt($this->ch, CURLOPT_PROXY, $proxy); return $this; } /** *设置请求的IP * *@paramtype$ip *@returntype */ public function set_ip($ip = '') { if (!empty($ip)) { curl_setopt($this->ch, CURLOPT_HTTPHEADER, array( "X-FORWARDED-FOR:$ip", "CLIENT-IP:$ip" )); } return $ip; } }
友情提示:关于这人php curl类其实就是把函数功能整理在一起了,然后写成一个class没有其它什么技巧了。
本文地址:http://www.phprm.com/code/73840.html
转载随意,但请附上文章地址:-)