@@ -21,29 +21,45 @@ class CurlHttp extends Component
2121 'User-Agent ' => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1 ' ,
2222 'Accept-Charset ' => 'GBK,utf-8 ' ,
2323 );
24-
24+
25+ private static $ methodDesc = [
26+ self ::METHOD_GET => "GET " ,
27+ self ::METHOD_POST => "POST " ,
28+ self ::METHOD_POSTJSON => "POST " ,
29+ ];
30+
2531 // closure
2632 public $ beforeRequest ;
2733 public $ afterRequest ;
2834
2935 private $ _curl ;
36+ private $ _action ;
3037
3138 public function init ()
3239 {
3340 parent ::init ();
3441 if (empty ($ this ->host )) {
3542 throw new InvalidParamException ("Please config host. " );
3643 }
37-
3844 }
3945
40- private function getUrl ()
46+ public function getUrl ()
4147 {
4248 $ url = $ this ->protocol .":// " .$ this ->host ;
4349 if ($ this ->port != 80 ) {
4450 $ url .= ": " .$ this ->port ;
4551 }
46- return $ url ;
52+ return $ url .$ this ->getAction ();
53+ }
54+
55+ public function getAction ()
56+ {
57+ return $ this ->_action ;
58+ }
59+
60+ public function setAction ($ action )
61+ {
62+ $ this ->_action = $ action ;
4763 }
4864
4965 public function setMethod ($ method )
@@ -59,7 +75,15 @@ public function setGet()
5975 }
6076 return $ this ->setMethod (self ::METHOD_GET );
6177 }
62-
78+
79+ public function getMethod ()
80+ {
81+ if (isset (self ::$ methodDesc [$ this ->method ])) {
82+ return self ::$ methodDesc [$ this ->method ];
83+ }
84+ return "GET " ;
85+ }
86+
6387 public function setPost ()
6488 {
6589 if (!empty ($ this ->headers ['Content-Type ' ])) {
@@ -108,19 +132,27 @@ public function getCurl()
108132 public function httpExec ($ action = "/ " , $ params = array ())
109133 {
110134 $ ch = $ this ->getCurl ();
135+ $ this ->setAction ($ action );
111136 if ($ this ->beforeRequest instanceof Closure) {
112137 $ params = call_user_func ($ this ->beforeRequest , $ params , $ this );
113138 empty ($ params ) && $ params = [];
114139 }
115- $ url = $ this ->getUrl (). $ action ;
140+ $ url = $ this ->getUrl ();
116141 if ($ this ->method == self ::METHOD_POST ) {
117142 curl_setopt ($ ch , CURLOPT_POST , 1 );
118143 curl_setopt ($ ch , CURLOPT_POSTFIELDS , http_build_query ($ params ));
119144 } elseif ($ this ->method == self ::METHOD_POSTJSON ) {
120145 curl_setopt ($ ch , CURLOPT_POST , 1 );
121146 curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ params ));
122147 } else {
123- !empty ($ params ) && $ url .= "? " .http_build_query ($ params );
148+ if (!empty ($ params )) {
149+ $ temp = explode ("? " , $ url );
150+ if (count ($ temp ) > 1 ) {
151+ $ url = $ temp [0 ]."? " .$ temp [1 ].'& ' .http_build_query ($ params );
152+ } else {
153+ $ url = $ url ."? " .http_build_query ($ params );
154+ }
155+ }
124156 }
125157 curl_setopt ($ ch , CURLOPT_TIMEOUT , $ this ->timeout );
126158 curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , $ this ->connectTimeout );
0 commit comments