11<?php
2+
23namespace lspbupt \curl ;
4+
35use Yii ;
4- use \ yii \helpers \ArrayHelper ;
5- use \ yii \base \Component ;
6- use \ yii \base \InvalidParamException ;
7- use Closure ;
6+ use yii \helpers \ArrayHelper ;
7+ use yii \base \Component ;
8+ use yii \base \InvalidParamException ;
9+
810/*encapsulate normal Http Request*/
911class BaseCurlHttp extends Component
1012{
@@ -16,40 +18,40 @@ class BaseCurlHttp extends Component
1618 public $ connectTimeout = 5 ;
1719 public $ returnTransfer = 1 ;
1820 public $ followLocation = 1 ;
19- public $ protocol = " http " ;
21+ public $ protocol = ' http ' ;
2022 public $ port = 80 ;
2123 public $ host ;
2224 public $ method = self ::METHOD_GET ;
2325 public $ headers = array (
2426 'User-Agent ' => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1 ' ,
25- 'Accept-Charset ' => 'GBK,utf-8 ' ,
27+ 'Accept-Charset ' => 'GBK,utf-8 ' ,
2628 );
2729 public $ action ;
2830 public $ params ;
2931 private $ debug = false ;
3032 //默认为非formData的模式,传文件时需要开启
3133 private $ isFormData = false ;
3234 private static $ methodDesc = [
33- self ::METHOD_GET => " GET " ,
34- self ::METHOD_POST => " POST " ,
35- self ::METHOD_POSTJSON => " POST " ,
35+ self ::METHOD_GET => ' GET ' ,
36+ self ::METHOD_POST => ' POST ' ,
37+ self ::METHOD_POSTJSON => ' POST ' ,
3638 ];
3739
3840 private $ _curl ;
3941
4042 public function init ()
4143 {
4244 parent ::init ();
43- if (empty ($ this ->host )) {
44- throw new InvalidParamException (" Please config host. " );
45+ if (empty ($ this ->host )) {
46+ throw new InvalidParamException (' Please config host. ' );
4547 }
4648 }
4749
4850 public function getUrl ()
4951 {
50- $ url = $ this ->protocol ." :// " .$ this ->host ;
51- if ($ this ->port != 80 ) {
52- $ url .= " : " .$ this ->port ;
52+ $ url = $ this ->protocol .' :// ' .$ this ->host ;
53+ if ($ this ->port != 80 ) {
54+ $ url .= ' : ' .$ this ->port ;
5355 }
5456 return $ url .$ this ->getAction ();
5557 }
@@ -82,24 +84,24 @@ public function setMethod($method)
8284
8385 public function setGet ()
8486 {
85- if (!empty ($ this ->headers ['Content-Type ' ])) {
86- unset($ this ->headers [" Content-Type " ]);
87+ if (!empty ($ this ->headers ['Content-Type ' ])) {
88+ unset($ this ->headers [' Content-Type ' ]);
8789 }
8890 return $ this ->setMethod (self ::METHOD_GET );
8991 }
9092
9193 public function getMethod ()
9294 {
93- if (isset (self ::$ methodDesc [$ this ->method ])) {
95+ if (isset (self ::$ methodDesc [$ this ->method ])) {
9496 return self ::$ methodDesc [$ this ->method ];
9597 }
96- return " GET " ;
98+ return ' GET ' ;
9799 }
98100
99101 public function setPost ()
100102 {
101- if (!empty ($ this ->headers ['Content-Type ' ])) {
102- unset($ this ->headers [" Content-Type " ]);
103+ if (!empty ($ this ->headers ['Content-Type ' ])) {
104+ unset($ this ->headers [' Content-Type ' ]);
103105 }
104106 return $ this ->setMethod (self ::METHOD_POST );
105107 }
@@ -110,31 +112,44 @@ public function setPostJson()
110112 return $ this ->setMethod (self ::METHOD_POSTJSON );
111113 }
112114
113-
114115 public function setProtocol ($ protocol )
115116 {
116117 $ this ->protocol = $ protocol ;
117118 return $ this ;
118119 }
119120
121+ public function setHeaders ($ arr = [])
122+ {
123+ if (!ArrayHelper::isIndexed ($ arr )) {
124+ foreach ($ arr as $ key => $ value ) {
125+ $ this ->setHeader ($ key , $ value );
126+ }
127+ }
128+ return $ this ;
129+ }
130+
120131 public function setHeader ($ key , $ value )
121132 {
122- $ this ->headers [$ key ] = $ value ;
133+ if ($ value === null ) {
134+ unset($ this ->headers [$ key ]);
135+ } else {
136+ $ this ->headers [$ key ] = $ value ;
137+ }
123138 return $ this ;
124139 }
125140
126141 private function getHeads ()
127142 {
128143 $ heads = [];
129- foreach ($ this ->headers as $ key => $ val ) {
130- $ heads [] = $ key ." : " .$ val ;
144+ foreach ($ this ->headers as $ key => $ val ) {
145+ $ heads [] = $ key .' : ' .$ val ;
131146 }
132147 return $ heads ;
133148 }
134149
135150 public function getCurl ()
136151 {
137- if ($ this ->_curl ) {
152+ if ($ this ->_curl ) {
138153 return $ this ->_curl ;
139154 }
140155 $ this ->_curl = curl_init ();
@@ -146,7 +161,7 @@ public function setDebug($debug = true)
146161 $ this ->debug = $ debug ;
147162 return $ this ;
148163 }
149-
164+
150165 public function setFormData ($ isFormData = true )
151166 {
152167 $ this ->isFormData = $ isFormData ;
@@ -156,8 +171,13 @@ public function setFormData($isFormData = true)
156171 public function isDebug ()
157172 {
158173 return $ this ->debug ;
159- }
174+ }
160175
176+ public function setOpt ($ option , $ value )
177+ {
178+ curl_setopt ($ this ->getCurl (), $ option , $ value );
179+ return $ this ;
180+ }
161181
162182 //请求之前的操作
163183 protected function beforeCurl ($ params )
@@ -171,78 +191,103 @@ protected function afterCurl($data)
171191 return $ data ;
172192 }
173193
174- public function httpExec ($ action = "/ " , $ params = [])
194+ /**
195+ * @deprecated 推荐使用send方法
196+ *
197+ * @param string $action
198+ * @param array $params
199+ */
200+ public function httpExec ($ action = '/ ' , $ params = [])
201+ {
202+ return $ this ->send ($ action , $ params );
203+ }
204+
205+ public function send ($ action = '/ ' , $ params = [])
175206 {
176207 $ this ->setAction ($ action );
177208 $ this ->setParams ($ params );
178- if ($ this ->isDebug ()) {
209+ if ($ this ->isDebug ()) {
179210 echo "\n开始请求之前: \nurl: " .$ this ->getUrl ()."\n参数列表: " .json_encode ($ this ->getParams ())."\n方法: " .$ this ->getMethod ()."\n" ;
180211 }
181212 $ ret = $ this ->beforeCurl ($ params );
182- if (!$ ret ) {
183- return "" ;
213+ if (!$ ret ) {
214+ return '' ;
184215 }
185216 $ ch = $ this ->getCurl ();
186217 $ url = $ this ->getUrl ();
187218 if ($ this ->method == self ::METHOD_POST ) {
188219 curl_setopt ($ ch , CURLOPT_POST , 1 );
189- if ($ this ->isFormData ) {
220+ if ($ this ->isFormData ) {
190221 curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ this ->getParams ());
191- }else {
222+ } else {
192223 curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ this ->getParams ()));
193224 }
194225 } elseif ($ this ->method == self ::METHOD_POSTJSON ) {
195226 curl_setopt ($ ch , CURLOPT_POST , 1 );
196227 curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ this ->getParams ()));
197228 } else {
198- if (!empty ($ params )) {
199- $ temp = explode (" ? " , $ url );
200- if (count ($ temp ) > 1 ) {
201- $ url = $ temp [0 ]." ? " .$ temp [1 ].'& ' .http_build_query ($ this ->getParams ());
229+ if (!empty ($ params )) {
230+ $ temp = explode (' ? ' , $ url );
231+ if (count ($ temp ) > 1 ) {
232+ $ url = $ temp [0 ].' ? ' .$ temp [1 ].'& ' .http_build_query ($ this ->getParams ());
202233 } else {
203- $ url = $ url ." ? " .http_build_query ($ this ->getParams ());
234+ $ url = $ url .' ? ' .http_build_query ($ this ->getParams ());
204235 }
205236 }
206237 }
207- if ($ this ->isDebug ()) {
238+ if ($ this ->isDebug ()) {
208239 echo "\n开始请求: \nurl: $ {url}\n参数列表: " .json_encode ($ this ->getParams ())."\n方法: " .$ this ->getMethod ()."\n" ;
209240 }
210241 curl_setopt ($ ch , CURLOPT_TIMEOUT , $ this ->timeout );
211242 curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , $ this ->connectTimeout );
212243 curl_setopt ($ ch , CURLOPT_URL , $ url );
213244 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , $ this ->returnTransfer );
214- curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ this ->getHeads ());
245+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ this ->getHeads ());
215246 curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , $ this ->followLocation );
247+ $ this ->beforeCurlExec ($ ch );
216248 $ data = curl_exec ($ ch );
217- if ($ this ->isDebug ()) {
249+ $ this ->afterCurlExec ($ ch );
250+ if ($ this ->isDebug ()) {
218251 echo "\n请求结果: " .$ data ."\n" ;
219252 }
220253 $ data = $ this ->afterCurl ($ data );
221254 curl_close ($ ch );
222- $ this ->_curl = null ;
255+ $ this ->refreshCurl () ;
223256 return $ data ;
224257 }
225258
226- public static function requestByUrl ($ url , $ params = [], $ method =self ::METHOD_GET )
259+ public function beforeCurlExec (&$ ch )
260+ {
261+ }
262+
263+ public function afterCurlExec (&$ ch )
264+ {
265+ }
266+
267+ public function refreshCurl ()
268+ {
269+ $ this ->_curl = null ;
270+ }
271+
272+ public static function requestByUrl ($ url , $ params = [], $ method = self ::METHOD_GET )
227273 {
228274 $ data = parse_url ($ url );
229275 $ config = [];
230- $ config ['protocol ' ] = ArrayHelper::getValue ($ data , " scheme " , " http " );
231- $ config ['host ' ] = ArrayHelper::getValue ($ data , " host " , "" );
232- $ config ['port ' ] = ArrayHelper::getValue ($ data , " port " , 80 );
276+ $ config ['protocol ' ] = ArrayHelper::getValue ($ data , ' scheme ' , ' http ' );
277+ $ config ['host ' ] = ArrayHelper::getValue ($ data , ' host ' , '' );
278+ $ config ['port ' ] = ArrayHelper::getValue ($ data , ' port ' , 80 );
233279 $ config ['method ' ] = $ method ;
234- $ action = ArrayHelper::getValue ($ data , " path " , "" );
235- $ queryStr = ArrayHelper::getValue ($ data , " query " , "" );
236- $ fragment = ArrayHelper::getValue ($ data , " fragment " , "" );
237- if ($ queryStr ) {
238- $ action .= " ? " .$ queryStr ;
280+ $ action = ArrayHelper::getValue ($ data , ' path ' , '' );
281+ $ queryStr = ArrayHelper::getValue ($ data , ' query ' , '' );
282+ $ fragment = ArrayHelper::getValue ($ data , ' fragment ' , '' );
283+ if ($ queryStr ) {
284+ $ action .= ' ? ' .$ queryStr ;
239285 }
240- if ($ fragment ) {
241- $ action .= " # " .$ fragment ;
286+ if ($ fragment ) {
287+ $ action .= ' # ' .$ fragment ;
242288 }
243289 $ config ['class ' ] = get_called_class ();
244290 $ obj = Yii::createObject ($ config );
245291 return $ obj ->httpExec ($ action , $ params );
246292 }
247-
248293}
0 commit comments