@@ -64,21 +64,54 @@ public function __construct($retryTimes = 1) {
6464 * GET方式的请求
6565 * @param string $url 请求的链接
6666 * @param int $timeoutMs 超时设置,单位:毫秒
67- * @return string 接口返回的内容,超时返回false
67+ * @return string|boolean 接口返回的内容,超时返回false
6868 */
6969 public function get ($ url , $ timeoutMs = 3000 ) {
70- return $ this ->request ($ url , array (), $ timeoutMs );
70+ return $ this ->request ($ url , array (), $ timeoutMs, ' GET ' );
7171 }
7272
7373 /**
7474 * POST方式的请求
7575 * @param string $url 请求的链接
7676 * @param array $data POST的数据
7777 * @param int $timeoutMs 超时设置,单位:毫秒
78- * @return string 接口返回的内容,超时返回false
78+ * @return string|boolean 接口返回的内容,超时返回false
7979 */
8080 public function post ($ url , $ data , $ timeoutMs = 3000 ) {
81- return $ this ->request ($ url , $ data , $ timeoutMs );
81+ return $ this ->request ($ url , $ data , $ timeoutMs , 'POST ' );
82+ }
83+
84+ /**
85+ * PUT方式的请求
86+ * @param string $url 请求的链接
87+ * @param array $data PUT的数据
88+ * @param int $timeoutMs 超时设置,单位:毫秒
89+ * @return string|boolean 接口返回的内容,超时返回false
90+ */
91+ public function put ($ url , $ data , $ timeoutMs = 3000 ) {
92+ return $ this ->request ($ url , $ data , $ timeoutMs , 'PUT ' );
93+ }
94+
95+ /**
96+ * DELETE方式的请求
97+ * @param string $url 请求的链接
98+ * @param array $data DELETE的数据
99+ * @param int $timeoutMs 超时设置,单位:毫秒
100+ * @return string|boolean 接口返回的内容,超时返回false
101+ */
102+ public function delete ($ url , $ data , $ timeoutMs = 3000 ) {
103+ return $ this ->request ($ url , $ data , $ timeoutMs , 'DELETE ' );
104+ }
105+
106+ /**
107+ * PATCH方式的请求
108+ * @param string $url 请求的链接
109+ * @param array $data PATCH的数据
110+ * @param int $timeoutMs 超时设置,单位:毫秒
111+ * @return string|boolean 接口返回的内容,超时返回false
112+ */
113+ public function patch ($ url , $ data , $ timeoutMs = 3000 ) {
114+ return $ this ->request ($ url , $ data , $ timeoutMs , 'PATCH ' );
82115 }
83116
84117 /** ------------------ 前置方法 ------------------ **/
@@ -159,26 +192,36 @@ public function withCookies() {
159192 * @param string $url 请求的链接
160193 * @param array $data POST的数据
161194 * @param int $timeoutMs 超时设置,单位:毫秒
162- * @return string 接口返回的内容,超时返回false,异常取消抛出时返回NULL
195+ * @param string $requestMethod 请求方式,例如:GET/POST/PUT/DELETE,不确定服务器支持这个自定义方法则不要使用它。
196+ * @return string 接口返回的内容,超时返回false,异常取消抛出时返回NULL
163197 * @throws Exception
164198 */
165- protected function request ($ url , $ data , $ timeoutMs = 3000 ) {
199+ public function request ($ url , $ data , $ timeoutMs = 3000 , $ requestMethod = NULL ) {
166200 $ rs = NULL ;
167201
168202 $ options = array (
169203 CURLOPT_URL => $ url ,
170204 CURLOPT_RETURNTRANSFER => TRUE ,
171205 CURLOPT_HEADER => 0 ,
206+ CURLOPT_TIMEOUT_MS => $ timeoutMs ,
172207 CURLOPT_CONNECTTIMEOUT_MS => $ timeoutMs ,
173208 CURLOPT_HTTPHEADER => $ this ->getHeaders (),
174209 );
175210
211+ // 请求方式
212+ $ requestMethod = strtoupper ($ requestMethod );
213+ if ($ requestMethod ) {
214+ $ options [CURLOPT_CUSTOMREQUEST ] = $ requestMethod ;
215+ }
216+ if ($ requestMethod == 'POST ' ) {
217+ $ options [CURLOPT_POST ] = TRUE ;
218+ }
219+
176220 if (!empty ($ data )) {
177- $ options [CURLOPT_POST ] = 1 ;
178- $ options [CURLOPT_POSTFIELDS ] = $ data ;
221+ $ options [CURLOPT_POSTFIELDS ] = $ data ;
179222 }
180223
181- $ options = $ this ->option + $ options ; //$this->>option优先
224+ $ options = $ this ->option + $ options ; // $this->>option优先
182225
183226 $ ch = curl_init ();
184227 curl_setopt_array ($ ch , $ options );
@@ -189,17 +232,18 @@ protected function request($url, $data, $timeoutMs = 3000) {
189232 } while ($ rs === FALSE && $ curRetryTimes >= 0 );
190233 $ errno = curl_errno ($ ch );
191234 if ($ errno && $ this ->isThrowExcption ) {
192- throw new InternalServerErrorException (sprintf ("%s::%s( %d) \n" , $ url , curl_error ($ ch ), $ errno ));
235+ throw new InternalServerErrorException (sprintf ("%s %s (Curl error: %d) \n" , $ url , curl_error ($ ch ), $ errno ));
193236 }
194237
195- //update cookie
238+ // update cookie
196239 if ($ this ->hascookie ) {
197240 $ cookie = $ this ->getRetCookie (curl_getinfo ($ ch , CURLINFO_COOKIELIST ));
198241 !empty ($ cookie ) && $ this ->cookie = $ cookie + $ this ->cookie ;
199242 $ this ->hascookie = FALSE ;
200243 unset($ this ->header ['Cookie ' ]);
201244 unset($ this ->option [CURLOPT_COOKIEFILE ]);
202245 }
246+
203247 curl_close ($ ch );
204248
205249 return $ rs ;
0 commit comments