CURL函数扩展及测试

来源:互联网 发布:锤子移动的效果js 编辑:程序博客网 时间:2024/06/06 03:24

原创:http://www.phpthinking.com/archives/220


001<?php
002/**
003* @description: 封装CURL扩展
004* @date: 2014-07-28 16:04
005*/
006 
007/**
008* @编码规范
009* @class 类名首字母大写,类名为多个单词, 每个大字首字母大写 eg: class Curl , class CurlPage
010* @variable 变量名小写, 变量名为多个单词, 每个单词小写,使用下划线_分割 eg: $curl_result
011* @function 函数名与类名规则相同 eg: function SendRequest
012* @params 函数形参规则与变量名相同
013* @class-variable 成员变量,以下划线结尾,多个单词使用下划线分隔. eg: private $host_name_
014*/
015/**
016* @要求
017*
018*/
019classCurl{
020 
021/**
022* @请求的host
023*/
024private$host_;
025 
026/**
027* @curl 句柄
028*/
029private$ch_;
030 
031/**
032* @请求的设置
033*/
034private$options_;
035 
036/**
037* @保存请求头信息
038*/
039private$request_header_;
040 
041/**
042* @保存响应头信息
043*/
044private$response_header_;
045 
046/**
047* @body_ 用于保存curl请求返回的结果
048*/
049private$body_;
050 
051/**
052* @读取cookie
053*/
054private$cookie_file_;
055 
056/**
057* @写入cookie
058*/
059private$cookie_jar_;
060 
061/**
062* @todo proxy
063* @构造函数,初始化CURL回话
064*/
065publicfunction __construct(){
066$this->ch_ = curl_init();
067curl_setopt($this->ch_, CURLOPT_COOKIE, 1);
068curl_setopt($this->ch_, CURLOPT_HEADER, 1);
069curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 1 );
070}
071 
072/**
073* @返回响应头
074*/
075publicfunction ResponseHeader($time_=5){
076if(!function_exists('http_parse_headers')) {
077functionhttp_parse_headers ($raw_headers){
078$headers= array();
079foreach(explode("\n",$raw_headers)as$i => $h) {
080$h= explode(':',$h, 2);
081if(isset($h[1])) {
082if(!isset($headers[$h[0]])) {
083$headers[$h[0]] = trim($h[1]);
084}elseif(is_array($headers[$h[0]])) {
085$tmp= array_merge($headers[$h[0]],array(trim($h[1])));
086$headers[$h[0]] = $tmp;
087}else{
088$tmp= array_merge(array($headers[$h[0]]),array(trim($h[1])));
089$headers[$h[0]] = $tmp;
090}
091}
092}
093return$headers;
094}
095}
096curl_setopt($this->ch_, CURLOPT_TIMEOUT, $time_);
097$header_size= curl_getinfo($this->ch_, CURLINFO_HEADER_SIZE);
098$this->response_header_ = substr($this->body_,$start= 0, $offset= $header_size);
099$this->response_header_ = http_parse_headers($this->response_header_);
100print_r($this->response_header_);
101}
102/**
103* @读取cookie
104*/
105publicfunction SetCookieFile($url,$cookie_file){
106curl_setopt($this->ch_, CURLOPT_URL, $url);
107curl_setopt($this->ch_, CURLOPT_COOKIEFILE , $cookie_file);
108$this->body_=$this->Execut();
109return$this->Close($this->body_);
110}
111 
112/**
113* @写入cookie
114*/
115publicfunction SetCookieJar($url,$cookie_jar){
116curl_setopt($this->ch_, CURLOPT_URL, $url);
117curl_setopt($this->ch_, CURLOPT_COOKIEJAR , $cookie_jar);
118$this->body_=$this->Execut();
119return$this->Close($this->body_);
120}
121 
122/**
123* @设置HEADER
124*/
125 
126publicfunction SetHeader($headers= null){
127if(is_array($headers) && count($headers) > 0) {
128curl_setopt($this->ch_, CURLOPT_HTTPHEADER, $headers);
129}
130}
131 
132/**
133* @GET请求
134*/
135publicfunction Get($url,$time_=5,$response=false,array$params = array()) {
136if($params) {
137if(strpos($url,'?')) {
138$url.= "&".http_build_query($params);
139}
140else{
141$url.= "?".http_build_query($params);
142}
143}
144curl_setopt($this->ch_, CURLOPT_URL, $url);
145curl_setopt($this->ch_, CURLOPT_TIMEOUT, $time_);
146if(strpos($url,'https') === 0) {
147curl_setopt($this->ch_, CURLOPT_SSL_VERIFYHOST, 0);
148curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);
149}
150$this->body_=$this->Execut();
151if($response){
152$this->ResponseHeader($time_);
153}
154return$this->Close($this->body_);
155}
156 
157/**
158* @POST请求
159*/
160publicfunction Post($url,$time_=5,$response=false,array$params = array()) {
161curl_setopt($this->ch_, CURLOPT_URL, $url);
162curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);
163curl_setopt($this->ch_, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
164curl_setopt($this->ch_, CURLOPT_POST, true);
165curl_setopt($this->ch_, CURLOPT_TIMEOUT, $time_);
166if($params) {
167curl_setopt($this->ch_, CURLOPT_POSTFIELDS, http_build_query($params));
168}
169$this->body_=$this->Execut();
170if($response){
171$this->ResponseHeader($time_);
172}
173return$this->Close($this->body_);
174}
175 
176/**
177* @tips: google http head 方法
178*/
179publicfunction Head($url,$time_=5,$response=false,array$params = array()) {
180curl_setopt($this->ch_, CURLOPT_URL, $url);
181curl_setopt($this->ch_, CURLOPT_TIMEOUT, $time_);
182curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 0);
183curl_setOpt($this->ch_,CURLOPT_NOBODY, true);
184$this->body_=$this->Execut();
185if($response){
186$this->ResponseHeader($time_);
187}
188return$this->Close($this->body_);
189}
190 
191/**
192* @执行CURL会话
193*/
194publicfunction Execut(){
195returncurl_exec($this->ch_);
196}
197 
198/**
199* @关闭CURL句柄
200*/
201publicfunction Close($body_){
202if($body_=== false) {
203$fh= fopen('log_error.txt',"w");
204$log_error=curl_error($this->ch_);
205fwrite($fh,"CURL Error: " .$log_error);
206fclose($fh);
207returnfalse;
208}
209curl_close($this->ch_);
210return$body_;
211}
212}
213 
214//测试数据
215 
216$x=newCurl();
217 
218$x->Post('http://www.baidu.com','',true);
219//$x->SetCookieJar("http://www.baidu.com/index.php?tn=19045005_27_pg",'');
220//$x->ResponseHeader('http://www.baidu.com');
221//$x->Head('http://www.baidu.com');
222//$x->Get('http://www.baidu.com');
223//$x->Post('http://www.baidu.com');

2 0
原创粉丝点击