Conditions | 14 |
Paths | 224 |
Total Lines | 72 |
Code Lines | 40 |
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 |
||
40 | static public function httpPost(string $url, array $param = array(), array $httpHeaders = array(), string $proxy='', int $http_code = 200) |
||
41 | { |
||
42 | /** 参数检测,object或者array进行http_build_query */ |
||
43 | if(!empty($param) && is_array($param)){ |
||
44 | $flag = false; |
||
45 | foreach ($param as $value){ |
||
46 | //判断参数是否是一个对象 或者 是一个数组 |
||
47 | if(is_array($value) || (is_string($value) && is_object($value))){ |
||
48 | $flag = true; |
||
49 | break; |
||
50 | } |
||
51 | } |
||
52 | if($flag){ |
||
53 | $param = http_build_query($param); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | $curl = curl_init(); |
||
58 | |||
59 | /** 设置请求链接 */ |
||
60 | curl_setopt($curl, CURLOPT_URL, $url); |
||
61 | curl_setopt($curl, CURLOPT_HEADER, 0); |
||
62 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
63 | |||
64 | /** 设置请求参数 */ |
||
65 | curl_setopt($curl, CURLOPT_POST, true); |
||
66 | curl_setopt($curl, CURLOPT_POSTFIELDS, $param); |
||
67 | |||
68 | /** 设置请求headers */ |
||
69 | if(empty($httpHeaders)) $httpHeaders = self::$httpHeaders; |
||
70 | curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeaders); |
||
71 | |||
72 | /** gzip压缩 */ |
||
73 | curl_setopt($curl, CURLOPT_ACCEPT_ENCODING, "gzip,deflate"); |
||
74 | |||
75 | /** 不验证https证书和hosts */ |
||
76 | if (stripos($url, "https://") !== FALSE) { |
||
77 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); |
||
78 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); |
||
79 | curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); |
||
80 | } |
||
81 | |||
82 | /** http代理 */ |
||
83 | if(!empty($proxy)){ |
||
84 | $proxy = explode(':',$proxy); |
||
85 | curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式 |
||
86 | curl_setopt($curl, CURLOPT_PROXY, "$proxy[0]"); //代理服务器地址 |
||
87 | curl_setopt($curl, CURLOPT_PROXYPORT,$proxy[1]); //代理服务器端口 |
||
88 | } |
||
89 | |||
90 | /** 请求 */ |
||
91 | $content = curl_exec($curl); |
||
92 | /** 获取请求信息 */ |
||
93 | $info = curl_getinfo($curl); |
||
94 | /** 关闭请求资源 */ |
||
95 | curl_close($curl); |
||
96 | |||
97 | if($http_code !== 0){ |
||
98 | /** 验证网络请求状态 */ |
||
99 | if (intval($info["http_code"]) === 0) { |
||
100 | throw new TException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD, |
||
101 | '[httpPost]: POST request was aborted ! Request url :' . $url . ' , post request data : ' . var_export($param,true) |
||
102 | ); |
||
103 | }elseif(intval($info["http_code"]) != $http_code){ |
||
104 | throw new TException(StatusCode::COMMON_TINYMENG_REQUEST_METHOD, |
||
105 | '[httpPost]: POST request was aborted ! Request url :' . $url . ' , post request data : ' . var_export($param,true).' ,return code : '.$info["http_code"] .' ,return content : '.$content |
||
|
|||
106 | ); |
||
107 | } else { |
||
108 | return $content; |
||
109 | } |
||
110 | }else{ |
||
111 | return $content; |
||
112 | } |
||
189 |