php的curl功能使用之前,首先我们要开启curl模块,如果是windows操作系统只需要打开php_curl.dll既可,如果是linux我们可以单独编译curl然后修改配置文件增加curl.so模块即可,或者重新编译加上选项–with-curl[=DIR];具体的curl安装就不详细j介绍了。有一点要注意要在Windows环境下使用这个模块,libeay32.dll和ssleay32.dll必须放到PATH环境变量包含的目录下。 不用cURL网站上的libcurl.dll。
开启cURL之后php增加的函数如下:
■cURL 函数curl_close — 关闭一个cURL会话
■curl_copy_handle — 复制一个cURL句柄和它的所有选项
■curl_errno — 返回最后一次的错误号
■curl_error — 返回一个保护当前会话最近一次错误的字符串
■curl_exec — 执行一个cURL会话
■curl_getinfo — 获取一个cURL连接资源句柄的信息
■curl_init — 初始化一个cURL会话
■curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄
■curl_multi_close — 关闭一组cURL句柄
■curl_multi_exec — 解析一个cURL批处理句柄
■curl_multi_getcontent — 如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流
■curl_multi_info_read — 获取当前解析的cURL的相关传输信息
■curl_multi_init — 返回一个新cURL批处理句柄
■curl_multi_remove_handle — 移除curl批处理句柄资源中的某个句柄资源
■curl_multi_select — 等待所有cURL批处理中的活动连接
■curl_setopt_array — 为cURL传输会话批量设置选项
■curl_setopt — 设置一个cURL传输选项
■curl_version — 获取cURL版本信息
我们可以通过答应函数 get_defined_functions返回的值知道,这些函数表面意思很明确,如果灵活的运用才是今天我们重点要说的内容,curl通常用于php请求接口,返回数据php处理执行不同的流程,这就涉及到服务端api的编写(这里网上可以搜集到很多api,开放平台最基本的就是api数据,例如open.taobao.com)。基本开源框架中都会有相应的curl类库,下面是个个人编写的curl类库,仅供实例教程参考。
平板视图
打印?
001
classcurl
002
{
003
var$RequestType='http';
004
var$ReturnType='json';
005
var$SubmitType='get';
006
var$UrlHost='localhost';
007
var$File='';
008
var$Charset='UTF-8';
009
var$Cookies=array();
010
var$RequestData=array();
011
var$input=array();
012
function__construct($host='',$dir='',$request='get',$protocol='http')
013
{
014
global$_INPUT;
015
$this->input =$_INPUT;
016
$this->setUrlHost($host,$dir);
017
$this->setRequestType($protocol);
018
$this->setSubmitType($request);
019
}
020
021
function__destruct()
022
{
023
}
024
025
publicfunctionsetCharset($charset)
026
{
027
if($charset)
028
{
029
$this->Charset =$charset;
030
}
031
}
032
033
publicfunctionsetUser()
034
{
035
}
036
037
publicfunctioninitPostData()
038
{
039
$this->RequestData =array();
040
}
041
042
publicfunctionsetReturnFormat($format)
043
{
044
if(!in_array($format,array('json','xml','str')))
045
{
046
$format='json';
047
}
048
$this->ReturnType =$format;
049
}
050
051
publicfunctionsetUrlHost($host,$apidir)
052
{
053
$this->UrlHost =$host;
054
$this->ApiDir =$apidir;
055
}
056
publicfunctionsetRequestType($type)
057
{
058
$this->RequestType =$type;
059
}
060
061
publicfunctionsetSubmitType($type)
062
{
063
$this->SubmitType =$type;
064
}
065
066
publicfunctionaddCookie($name,$value)
067
{
068
$this->Cookies[] = COOKIE_PREFIX .$name.'='.$value;
069
}
070
071
publicfunctionaddFile($file)
072
{
073
if(isset($file))
074
{
075
foreach($fileas$var=>$val)
076
{
077
if(is_array($val['tmp_name']))
078
{
079
foreach($val['tmp_name']as$k=>$fname)
080
{
081
$this->RequestData[$var."[$k]"] ="@".$fname.';type='.$val['type'][$k] .';filename='. urlencode($val['name'][$k]);
082
}
083
}
084
else
085
{
086
$this->RequestData[$var] ="@".$val['tmp_name'] .';type='.$val['type'] .';filename='. urlencode($val['name']);
087
}
088
}
089
}
090
}
091
092
publicfunctionaddRequestData($name,$value)
093
{
094
$this->RequestData[$name] =$value;
095
}
096
097
publicfunctionPostContentType($type)
098
{
099
$this->PostContentType =$type;
100
}
101
102
publicfunctionrequest($file)
103
{
104
if('get'==$this->SubmitType &&$this->RequestData)
105
{
106
foreach($this->RequestData AS$k=>$v)
107
{
108
$para.='&'.$k.'='. ($v);
109
}
110
}
111
if(strpos($file,'?'))
112
{
113
$pachar='&';
114
}
115
else
116
{
117
$pachar='?';
118
}
119
$url=$this->RequestType .'://'.$this->UrlHost .'/'.$this->ApiDir .$file.$pachar.'format='.$this->ReturnType .$para;
120
if(DEBUG_MODE)
121
{
122
if(!$file)
123
{
124
$file='index';
125
}
126
hg_debug_tofile($url, 0,date('Y/m/d/') .$this->ApiDir,$file.'.txt');
127
hg_debug_tofile($this->RequestData, 1,date('Y/m/d/') .$this->ApiDir,$file.'.txt');
128
}
129
$ch= curl_init();
130
curl_setopt($ch, CURLOPT_URL,$url);
131
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
132
if($this->Cookies)
133
{
134
$cookies= implode(';',$this->Cookies);
135
136
curl_setopt($ch, CURLOPT_COOKIE,$cookies);
137
}
138
if($this->RequestType =='https')
139
{
140
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
141
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
142
}
143
if('post'==$this->SubmitType)
144
{
145
curl_setopt($ch, CURLOPT_POST, true);
146
if($this->PostContentType =='string')
147
{
148
$postdata='';
149
foreach($this->RequestData AS$k=>$v)
150
{
151
$postdata.='&'.$k.'='.$v;
152
}
153
}
154
else
155
{
156
$postdata=$this->RequestData;
157
}
158
curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
159
}
160
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
161
$ret= curl_exec($ch);
162
$head_info= curl_getinfo($ch);
163
curl_close($ch);
164
if($head_info['http_code']!= 200)
165
{
166
if(DEBUG_MODE)
167
{
168
hg_debug_tofile($head_info, 1,date('Y/m/d/') .$this->ApiDir,$file.'.txt');
169
}
170
return'';
171
}
172
if(DEBUG_MODE)
173
{
174
hg_debug_tofile($ret, 0,date('Y/m/d/') .$this->ApiDir,$file.'.txt');
175
}
176
if($ret=='null')
177
{
178
return'';
179
}
180
$func=$this->ReturnType .'ToArray';
181
$ret=$this->$func($ret);
182
183
return$ret;
184
}
185
privatefunctionjsonToArray($json)
186
{
187
return$ret= json_decode($json,true);
188
}
189
190
//用于将数组直接用json的方式提交到某一个地址
191
publicfunctioncurl_json($url,$data)
192
{
193
$ch= curl_init();
194
curl_setopt($ch, CURLOPT_URL,$url);
195
curl_setopt($ch, CURLOPT_POST, true);
196
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
197
curl_setopt($ch, CURLOPT_POSTFIELDS,'data='.json_encode($data));
198
$response= curl_exec($ch);
199
$head_info= curl_getinfo($ch);
200
if($head_info['http_code']!= 200)
201
{
202
$error=array('return'=>'fail');
203
returnjson_encode($error);
204
}
205
curl_close($ch);//关闭
206
return$response;
207
}
208
209
//直接提交文件到某一地址
210
publicfunctionpost_files($url)
211
{
212
$ch= curl_init();
213
curl_setopt($ch, CURLOPT_URL,$url);
214
curl_setopt($ch, CURLOPT_POST, true);
215
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
216
curl_setopt($ch, CURLOPT_POSTFIELDS,$this->RequestData);
217
$response= curl_exec($ch);
218
curl_close($ch);//关闭
219
return$response;
220
}
221
222
privatefunctionxmlToArray($xml)
223
{
224
return$xml;
225
}
226
227
privatefunctionstrToArray($str)
228
{
229
return$str;
230
}
231
}
猛看这个类库你可能有点茫然,但是对oop熟悉的同志,抓住其中的几个重点方法就看的很明白了。
总结如下:
1、核心方法是request,其他都是辅助设置函数。
2、如果是https请求需要设置curl的参数curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
3、我添加了调试功能,可以在调用这个类之前设置define(‘DEBUG_MODE’, TRUE);hg_debug_tofile这个函数自己写。
4、$_INPUT应该是你的处理的输入数据。
5、此类库只处理了返回json的格式数据,xml和str没处理,此处也应该是按需处理。
6、类库中使用到的常量含义可以参考php手册的curl参数含义。网上有好多中文手册,本站流量有限就不提供下载了。