| Conditions | 8 |
| Paths | 64 |
| Total Lines | 58 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 127 | static public function httpGet(string $url, array $param = array(), array $httpHeaders = array(), string $proxy= '', int $http_code = 200) |
||
| 128 | { |
||
| 129 | $curl = curl_init(); |
||
| 130 | |||
| 131 | /** 设置请求参数 */ |
||
| 132 | if (!empty($param)) { |
||
| 133 | $url = $url . '?' . http_build_query($param); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** 设置请求链接 */ |
||
| 137 | curl_setopt($curl, CURLOPT_URL, $url); |
||
| 138 | curl_setopt($curl, CURLOPT_HEADER, 0); |
||
| 139 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
| 140 | |||
| 141 | /** 不验证https证书和hosts */ |
||
| 142 | if (stripos($url, "https://") !== FALSE) { |
||
| 143 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); |
||
| 144 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); |
||
| 145 | curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** http代理 */ |
||
| 149 | if(!empty($proxy)){ |
||
| 150 | $proxy = explode(':',$proxy); |
||
| 151 | curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式 |
||
| 152 | curl_setopt($curl, CURLOPT_PROXY, "$proxy[0]"); //代理服务器地址 |
||
| 153 | curl_setopt($curl, CURLOPT_PROXYPORT,$proxy[1]); //代理服务器端口 |
||
| 154 | } |
||
| 155 | |||
| 156 | /** 设置请求headers */ |
||
| 157 | if(empty($httpHeaders)) $httpHeaders = self::$httpHeaders; |
||
| 158 | curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders); |
||
| 159 | |||
| 160 | /** gzip压缩 */ |
||
| 161 | curl_setopt($curl, CURLOPT_ACCEPT_ENCODING, "gzip,deflate"); |
||
| 162 | |||
| 163 | /** 请求 */ |
||
| 164 | $content = curl_exec($curl); |
||
| 165 | /** 获取请求信息 */ |
||
| 166 | $info = curl_getinfo($curl); |
||
| 167 | /** 关闭请求资源 */ |
||
| 168 | curl_close($curl); |
||
| 169 | |||
| 170 | /** 验证网络请求状态 */ |
||
| 171 | if($http_code !== 0){ |
||
| 172 | if (intval($info["http_code"]) === 0) { |
||
| 173 | throw new TException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD, |
||
| 174 | '[httpGet]: GET request was aborted ! Request url :' . $url |
||
| 175 | ); |
||
| 176 | }elseif(intval($info["http_code"]) != $http_code){ |
||
| 177 | throw new TException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD, |
||
| 178 | '[httpGet]: GET request was aborted ! Request url :' . $url .' ,return code : '.$info["http_code"] .' ,return content : '.$content |
||
| 179 | ); |
||
| 180 | } else { |
||
| 181 | return $content; |
||
| 182 | } |
||
| 183 | }else{ |
||
| 184 | return $content; |
||
| 185 | } |
||
| 189 |