@@ -7,9 +7,9 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | namespace zhangv\wechat\pay\util; |
| 9 | 9 | |
| 10 | -class HttpClient{ |
|
| 10 | +class HttpClient { |
|
| 11 | 11 | |
| 12 | - const GET = 'get',POST = 'post', DELETE = 'delete',PUT = 'put'; |
|
| 12 | + const GET = 'get', POST = 'post', DELETE = 'delete', PUT = 'put'; |
|
| 13 | 13 | private $instance = null; |
| 14 | 14 | private $errNo = null; |
| 15 | 15 | private $info = null; |
@@ -17,17 +17,17 @@ discard block |
||
| 17 | 17 | private $retry = 1; |
| 18 | 18 | private $tried = 0; |
| 19 | 19 | |
| 20 | - public function __construct($timeout = 1,$retry = 1) { |
|
| 20 | + public function __construct($timeout = 1, $retry = 1) { |
|
| 21 | 21 | $this->timeout = $timeout; |
| 22 | 22 | $this->retry = $retry; |
| 23 | 23 | $this->initInstance(); |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - public function initInstance(){ |
|
| 27 | - if(!$this->instance) { |
|
| 26 | + public function initInstance() { |
|
| 27 | + if (!$this->instance) { |
|
| 28 | 28 | $this->instance = curl_init(); |
| 29 | - if($this->timeout < 1) { |
|
| 30 | - curl_setopt($this->instance, CURLOPT_NOSIGNAL,1); |
|
| 29 | + if ($this->timeout < 1) { |
|
| 30 | + curl_setopt($this->instance, CURLOPT_NOSIGNAL, 1); |
|
| 31 | 31 | curl_setopt($this->instance, CURLOPT_TIMEOUT_MS, $this->timeout * 1000); |
| 32 | 32 | }else |
| 33 | 33 | curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($this->timeout)); |
@@ -37,26 +37,26 @@ discard block |
||
| 37 | 37 | } |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - public function get($url,$params = array(),$headers = array(),$opts = array()) { |
|
| 40 | + public function get($url, $params = array(), $headers = array(), $opts = array()) { |
|
| 41 | 41 | if (!$this->instance) $this->initInstance($this->timeout); |
| 42 | - if($params && count($params) > 0) $url .= '?' . http_build_query($params); |
|
| 42 | + if ($params && count($params) > 0) $url .= '?'.http_build_query($params); |
|
| 43 | 43 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 44 | 44 | curl_setopt($this->instance, CURLOPT_HTTPGET, true); |
| 45 | 45 | curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
| 46 | - curl_setopt_array($this->instance,$opts); |
|
| 46 | + curl_setopt_array($this->instance, $opts); |
|
| 47 | 47 | $result = $this->execute(); |
| 48 | 48 | curl_close($this->instance); |
| 49 | 49 | $this->instance = null; |
| 50 | 50 | return $result; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - public function post($url, $params = array(),$headers = array(),$opts = array()) { |
|
| 53 | + public function post($url, $params = array(), $headers = array(), $opts = array()) { |
|
| 54 | 54 | if (!$this->instance) $this->initInstance($this->timeout); |
| 55 | 55 | curl_setopt($this->instance, CURLOPT_URL, $url); |
| 56 | 56 | curl_setopt($this->instance, CURLOPT_POST, true); |
| 57 | 57 | curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params); |
| 58 | 58 | curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
| 59 | - curl_setopt_array($this->instance,$opts); |
|
| 59 | + curl_setopt_array($this->instance, $opts); |
|
| 60 | 60 | $result = $this->execute(); |
| 61 | 61 | curl_close($this->instance); |
| 62 | 62 | $this->instance = null; |
@@ -65,23 +65,23 @@ discard block |
||
| 65 | 65 | |
| 66 | 66 | private function execute() { |
| 67 | 67 | $this->tried = 0; |
| 68 | - do{ |
|
| 68 | + do { |
|
| 69 | 69 | $result = curl_exec($this->instance); |
| 70 | 70 | $this->errNo = curl_errno($this->instance); |
| 71 | - }while(!$result && $this->errNo === CURLE_OPERATION_TIMEDOUT && $this->tried++ < $this->retry); |
|
| 71 | + } while (!$result && $this->errNo === CURLE_OPERATION_TIMEDOUT && $this->tried++ < $this->retry); |
|
| 72 | 72 | $this->info = curl_getinfo($this->instance); |
| 73 | 73 | return $result; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - public function getInfo(){ |
|
| 76 | + public function getInfo() { |
|
| 77 | 77 | return $this->info; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - public function getErrorNo(){ |
|
| 80 | + public function getErrorNo() { |
|
| 81 | 81 | return $this->errNo; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - public function getTried(){ |
|
| 84 | + public function getTried() { |
|
| 85 | 85 | return $this->tried; |
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | \ No newline at end of file |