实用curl类

  1 <?php
  2 
  3 /**
  4  * CURL for http request
  5  */
  6 class LHttpCurl
  7 {
  8     const GET_REQUEST = 'GET';
  9     const POST_REQUEST = 'POST';
 10 
 11 
 12     const RESPONSE_OK_CODE = 200;
 13 
 14     protected $sRequestMethod;
 15     protected $sGateUrl;
 16     protected $sQueryString;
 17     protected $arrProxyOptions;
 18     protected $arrSslOptions;
 19     protected $arrHeaders;
 20     protected $arrCookies;
 21     protected $iTimeout;
 22     protected $iCurlErrno;
 23     protected $sCurlErrMsg;
 24     protected $iResponseCode;
 25     protected $sResponseBody;
 26     protected $arrCurlInfo;
 27 
 28     public function __construct()
 29     {
 30         $this->init();
 31     }
 32 
 33     protected function init()
 34     {
 35         $this->sRequestMethod = self::GET_REQUEST;
 36         $this->sGateUrl = 'http://localhost/cgi';
 37         $this->sQueryString = '';
 38         $this->arrProxyOptions = array();
 39         $this->arrSslOptions = array();
 40         $this->arrHeaders = array();
 41         $this->arrCookies = array();
 42         $this->iTimeout = 3;
 43         $this->iCurlErrno = 0;
 44         $this->sCurlErrMsg = '';
 45         $this->iResponseCode = 0;
 46         $this->sResponseBody = '';
 47         $this->arrCurlInfo = array();
 48     }
 49 
 50     /**
 51      * @param string $requestMethod LHttpClient::GET_REQUEST | LHttpClient::POST_REQUEST
 52      */
 53     public function setMethod($requestMethod)
 54     {
 55         $this->sRequestMethod = $requestMethod;
 56     }
 57 
 58     /**
 59      * @param string $gateUrl 请求接口地址
 60      */
 61     public function setGateURL($gateUrl)
 62     {
 63         $this->init();
 64 
 65         $this->sGateUrl = $gateUrl;
 66     }
 67 
 68     /**
 69      * 设置请求串, 使用http_build_query()生成
 70      * @param string $queryString
 71      */
 72     public function setQueryString($queryString)
 73     {
 74         $this->sQueryString = $queryString;
 75     }
 76 
 77     public function getQueryString()
 78     {
 79         return $this->sQueryString;
 80     }
 81 
 82     /**
 83      * 返回GET形式的请求URL
 84      * @return string 请求URL
 85      */
 86     public function getRequestURL()
 87     {
 88         if (!empty($this->sQueryString))
 89         {
 90             return $this->sGateUrl . '?' . $this->sQueryString;
 91         }
 92         else
 93         {
 94             return $this->sGateUrl;
 95         }
 96     }
 97 
 98     /**
 99      * 设置代理
100      * @param string $proxyHost CURLOPT_PROXY: "host[:port]"
101      * @param string $proxyAuth CURLOPT_PROXYUSERPWD: "user:pass"
102      * @param int $proxyType CURLPROXY_HTTP (default) or CURLPROXY_SOCKS5
103      * @param int $proxyAuthType CURLAUTH_BASIC and CURLAUTH_NTLM
104      * @param bool $tunnel CURLOPT_HTTPPROXYTUNNEL: 是否tunnel方式使用代理
105      */
106     public function setProxy($proxyHost, $proxyAuth=null,
107                              $proxyType=null, $proxyAuthType=null,
108                              $tunnel = false)
109     {
110         $this->arrProxyOptions['proxyhost'] = $proxyHost;
111         $this->arrProxyOptions['proxyauth'] = $proxyAuth;
112         $this->arrProxyOptions['proxytype'] = $proxyType;
113         $this->arrProxyOptions['proxyauthtype'] = $proxyAuthType;
114         $this->arrProxyOptions['tunnel'] = $tunnel;
115     }
116 
117     /**
118      * 设置证书信息, 证书文件包含了key
119      * @param string $certFile 证书文件路径(最好使用绝对路径)
120      * @param string $certPasswd 使用证书或key需要的密码
121      * @param string $certType "PEM"(默认) | "DER"
122      */
123     public function setSslCert($certFile, $certPasswd, $certType="PEM")
124     {
125         $this->arrSslOptions['cert'] = $certFile;
126         $this->arrSslOptions['certtype'] = $certType;
127         $this->arrSslOptions['certpasswd'] = $certPasswd;
128     }
129 
130     /**
131      * 设置CA证书路径
132      * @param string $caFile
133      */
134     public function setSslCaInfo($caFile)
135     {
136         $this->arrSslOptions['cainfo'] = $caFile;
137         $this->arrSslOptions['verifypeer'] = true;
138         $this->arrSslOptions['verifyhost'] = 2;
139     }
140 
141     /**
142      * 合并$headers到当前请求头
143      * @param array $headers array('Host' => 'www.xxx.com') : 'Host: www.xxx.com'
144      */
145     public function addHeaders($headers)
146     {
147         $this->arrHeaders = array_merge($this->arrHeaders, $headers);
148     }
149 
150     /**
151      * 合并$cookies到当前请求cookie
152      * @param array $cookies array('fruit' => 'apple', 'colour' => 'red') : 'Cookie: fruit=apple; colour=red'
153      */
154     public function addCookies($cookies)
155     {
156         $this->arrCookies = array_merge($this->arrCookies, $cookies);
157     }
158 
159     /**
160      * 设置请求超时
161      * @param int $timeout
162      */
163     public function setTimeout($timeout)
164     {
165         $this->iTimeout = $timeout;
166     }
167 
168     /**
169      * 初始化curl, 发送请求, 记录响应和错误.
170      *
171      * @return bool ('http_code' == 200)
172      */
173     public function execute()
174     {
175 
176         $ch = curl_init();
177 
178         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
179 
180         if ( $this->sRequestMethod == self::POST_REQUEST ) {
181             //发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样
182             curl_setopt($ch, CURLOPT_URL, $this->sGateUrl);
183             curl_setopt($ch, CURLOPT_POST, true);
184             curl_setopt($ch, CURLOPT_POSTFIELDS, $this->sQueryString);
185         } else {
186             curl_setopt($ch, CURLOPT_URL, $this->getRequestURL());
187         }
188 
189         if ( isset($this->arrProxyOptions['proxyhost']) ) //初始化代理相关设置
190         {
191             curl_setopt($ch, CURLOPT_PROXY, $this->arrProxyOptions['proxyhost']);
192             curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->arrProxyOptions['proxyauth']);
193             curl_setopt($ch, CURLOPT_PROXYTYPE, $this->arrProxyOptions['proxytype']);
194             curl_setopt($ch, CURLOPT_PROXYAUTH, $this->arrProxyOptions['proxyauthtype']);
195 
196             curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $this->arrProxyOptions['tunnel']);
197         }
198 
199         if ( isset($this->arrSslOptions['cert']) ) //初始化ssl证书相关设置
200         {
201             curl_setopt($ch, CURLOPT_SSLCERT, $this->arrSslOptions['cert']);
202             curl_setopt($ch, CURLOPT_SSLCERTTYPE, $this->arrSslOptions['certtype']);
203             curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $this->arrSslOptions['certpasswd']);
204         }
205 
206         if ( isset($this->arrSslOptions['cainfo']) ) //初始化CA相关设置
207         {
208             curl_setopt($ch, CURLOPT_CAINFO, $this->arrSslOptions['cainfo']);
209             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->arrSslOptions['verifypeer']);
210             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->arrSslOptions['verifyhost']);
211         } else {
212             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
213             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
214         }
215 
216         //设置Header
217         if ( !empty($this->arrHeaders) ) {
218             $headers = array();
219             foreach ($this->arrHeaders as $key => $value) {
220                 $headers[] = "$key: $value";
221             }
222             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
223         }
224 
225         //设置Cookie
226         if ( !empty($this->arrCookies) ) {
227             $cookies = array();
228             foreach ($this->arrCookies as $key => $value) {
229                 $encodedValue = urlencode($value);
230                 $cookies[]    = "$key=$encodedValue";
231             }
232             curl_setopt($ch, CURLOPT_COOKIE, implode('; ', $cookies));
233         }
234 
235         // 设置curl允许执行的最长秒数
236         curl_setopt($ch, CURLOPT_TIMEOUT, $this->iTimeout);
237 
238         // 执行操作
239         $this->sResponseBody = curl_exec($ch);
240         $this->iCurlErrno    = curl_errno($ch);
241         $this->sCurlErrMsg   = curl_error($ch);
242         $this->arrCurlInfo   = curl_getinfo($ch);
243         $this->iResponseCode = (isset($this->arrCurlInfo['http_code']) ?
244             intval($this->arrCurlInfo['http_code']) : 0);
245 
246         // 释放资源
247         curl_close($ch);
248 
249         //var_dump($this->iResponseCode);
250 
251         //return $this->sResponseBody;
252         return ($this->iResponseCode == 200 ? true : false);
253     }
254 
255     /**
256      * 返回curl执行的错误码
257      * @return int curl_errno()
258      */
259     public function getCurlErrno()
260     {
261         return $this->iCurlErrno;
262     }
263 
264     /**
265      * 返回curl执行的错误信息
266      * @return string curl_error()
267      */
268     public function getCurlErrMsg()
269     {
270         return $this->sCurlErrMsg;
271     }
272 
273     /**
274      * 返回响应的HTTP Code, 正常返回 200
275      * @return int
276      */
277     public function getResponseCode()
278     {
279         return $this->iResponseCode;
280     }
281 
282     /**
283      * 返回响应内容
284      * @return string
285      */
286     public function getResponseBody()
287     {
288         return $this->sResponseBody;
289     }
290 
291     /**
292      * 返回响应数据
293      * @return string
294      */
295     public function getArrCurlInfo()
296     {
297         return $this->arrCurlInfo;
298     }
299 
300 
301 
302 }

 

posted @ 2016-06-02 22:03  xx2016  阅读(178)  评论(0编辑  收藏  举报