1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| <?php class JSocket { const methodGet="GET"; const methodPost="POST"; const methodDelete="DELETE"; const methodPut="PUT"; const retFormatJson="JSON"; const retFormatXml="XML"; const retFormatText="Text";
private $ch; private $url; private $requestType; private $param=[]; private $timeout=10; private $header=[]; private $method=JSocket::methodGet; private $file; private $ssl; private $ret; private $retFormat=JSocket::retFormatJson; public function __construct() { $this->ch = curl_init(); } public function setRetFormat($retFormat){ $this->retFormat=$retFormat; return $this; }
public function setRequestType($requestType) { $this->requestType=$requestType; }
public function setUrl($url){ $this->url=$url; return $this; }
public function setParam($key,$value){ $this->param[$key]=$value; return $this; }
public function getParam(){ return $this->param; }
public function setTimeout($timeout){ $this->timeout=$timeout; return $this; }
public function setHeader($header){ $this->header[]=$header; return $this; }
public function setMethod($method){ $this->method=$method; return $this; }
public function setFile($key,$file){ $this->file[$key]=curl_file_create($file); return $this; }
public function ssl($ssl){ $this->ssl=$ssl; return $this; }
public function exe(){ curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); if($this->header){ curl_setopt($this->ch,CURLOPT_HTTPHEADER, $this->header); curl_setopt($this->ch,CURLOPT_HEADER,FALSE); } switch ($this->method) { case self::methodPost: curl_setopt($this->ch, CURLOPT_POST, TRUE); if($this->file){ curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->file); } if($this->param){ if ($this->requestType==self::retFormatJson){ curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($this->param)); }else if ($this->requestType==self::retFormatXml){ curl_setopt($this->ch, CURLOPT_POSTFIELDS, \DataBaseHelper::arrayToXml($this->param)); }else{ curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($this->param)); } } break; case self::methodDelete: curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); case self::methodGet: if($this->param){ $this->url = $this->url . (strpos($this->url, '?') ? '&' : '?') . http_build_query($this->param); } } if($this->ssl){ curl_setopt($this->ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($this->ch,CURLOPT_SSLCERT, $this->ssl['cert']); curl_setopt($this->ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($this->ch,CURLOPT_SSLKEY, $this->ssl['key']); curl_setopt($this->ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt($this->ch,CURLOPT_SSL_VERIFYHOST,2); curl_setopt($this->ch,CURLOPT_HEADER, FALSE); if (isset($this->ssl['ca'])){ curl_setopt($this->ch,CURLOPT_CAINFO, $this->ssl['ca']); } }else{ curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($this->ch, CURLOPT_URL, $this->url); $starttime=$this->microtimeFloat(); $this->ret = curl_exec($this->ch); $endtime = $this->microtimeFloat(); $errorNum=curl_errno($this->ch); $errorMsg=curl_error($this->ch); $runtime = number_format(($endtime-$starttime), 4).'s'; if($errorNum){ Log::error($this->url, ["param"=>$this->param,"result"=>$errorNum,"runtime"=>$runtime,"response"=>$this->ret]); Log::error("curl请求错误信息:".$errorMsg); return NULL; } $this->http_code = curl_getinfo($this->ch,CURLINFO_HTTP_CODE); curl_close ($this->ch); Log::info($this->url, ["param"=>$this->param,"status"=>$this->http_code,"runtime"=>$runtime,"response"=>$this->ret]); return $this; }
public function getRet(){ switch ($this->retFormat){ case JSocket::retFormatJson: return json_decode($this->ret,TRUE); break; case JSocket::retFormatXml: return \DataBaseHelper::xmlToArray($this->ret); break; } return $this->ret; } public function getHttpCode(){ return $this->http_code; }
public function microtimeFloat(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } }
|