| Conditions | 44 |
| Paths | > 20000 |
| Total Lines | 161 |
| Code Lines | 109 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 156 | public function request($opt) |
||
| 157 | { |
||
| 158 | if (!isset($opt['url'])) { |
||
| 159 | throw new Exception('URL needed', 1); |
||
| 160 | } |
||
| 161 | $msisdn = isset($_SESSION['telkomsel']['msisdn']) ? $_SESSION['telkomsel']['msisdn'] : 'default'; |
||
| 162 | //$verbose = __DIR__ . '/otp/http/' . $msisdn . '.' . substr(clean_string(urldecode(urldecode($opt['url']))), 0, 50) . '.txt'; |
||
| 163 | //file_put_contents($verbose, ''); |
||
| 164 | //$curl_log = fopen($verbose, 'a'); |
||
| 165 | $ch = curl_init(); |
||
| 166 | |||
| 167 | $result = ['request' => [], 'response' => []]; |
||
| 168 | if (isset($opt['postdata'])) { |
||
| 169 | if (is_array($opt['postdata'])) { |
||
| 170 | $opt['postdata'] = http_build_query($opt['postdata'], '', '&'); |
||
| 171 | } |
||
| 172 | curl_setopt($ch, CURLOPT_POSTFIELDS, $opt['postdata']); |
||
| 173 | $result['request']['postdata'] = $opt['postdata']; |
||
| 174 | $this->require_content_length = true; |
||
| 175 | } |
||
| 176 | if (isset($opt['headers']) && is_array($opt['headers'])) { |
||
| 177 | $headers = $opt['headers']; |
||
| 178 | if (isset($opt['headers_trim'])) { |
||
| 179 | $headers = array_map('trim', $headers); |
||
| 180 | $headers = array_filter($headers); |
||
| 181 | $headers = array_values($headers); |
||
| 182 | } |
||
| 183 | for ($i = 0; $i < count($headers); ++$i) { |
||
| 184 | $header = array_map('trim', explode(':', $headers[$i])); |
||
| 185 | $small_header = strtolower($header[0]); |
||
| 186 | if ('content-length' == $small_header && true === $this->require_content_length) { |
||
| 187 | $headers[$i] = $header[0] . ': ' . strlen($opt['postdata']); |
||
| 188 | } |
||
| 189 | if ('user-agent' == $small_header) { |
||
| 190 | curl_setopt($ch, CURLOPT_USERAGENT, $header[1]); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | //$this->DUMPNow($headers, $opt); |
||
| 194 | |||
| 195 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
| 196 | $result['request']['headers'] = $headers; |
||
| 197 | } |
||
| 198 | curl_setopt($ch, CURLOPT_URL, trim($opt['url'])); |
||
| 199 | $result['url'] = $opt['url']; |
||
| 200 | if (isset($opt['post']) && $opt['post']) { |
||
| 201 | curl_setopt($ch, CURLOPT_POST, 1); |
||
| 202 | } |
||
| 203 | |||
| 204 | //evj($_SESSION, isset($opt['cookie']) && true === $opt['cookie'] && isset($_SESSION['cookie'])); |
||
| 205 | if (isset($opt['cookie']) && true === $opt['cookie'] && isset($_SESSION['cookie'])) { |
||
| 206 | $cookie = isset($_SESSION['cookie']) ? $_SESSION['cookie'] : null; |
||
| 207 | if (is_string($opt['cookie']) && !empty($opt['cookie'])) { |
||
| 208 | $cookie = $opt['cookie']; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($cookie = file::file($cookie, '')) { |
||
| 212 | if (!file_exists($cookie)) { |
||
| 213 | throw new Exception("$cookie not exists", 1); |
||
| 214 | //file_put_contents($cookie, ''); |
||
| 215 | } |
||
| 216 | curl_setopt($ch, CURLOPT_COOKIEJAR, realpath($cookie)); |
||
| 217 | curl_setopt($ch, CURLOPT_COOKIEFILE, realpath($cookie)); |
||
| 218 | $result['request']['cookie-file'] = realpath($cookie); |
||
| 219 | $result['response']['cookie-file'] = realpath($cookie); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); |
||
| 223 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 224 | curl_setopt($ch, CURLOPT_HEADER, true); |
||
| 225 | //curl_setopt($ch, CURLOPT_VERBOSE, true); |
||
| 226 | //curl_setopt($ch, CURLOPT_STDERR, $curl_log); |
||
| 227 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
||
| 228 | //curl_setopt($ch, CURLOPT_ENCODING, ''); |
||
| 229 | if (isset($opt['setopt']) && is_array($opt['setopt']) && !empty($opt['setopt'])) { |
||
| 230 | foreach ($opt['setopt'] as $key => $value) { |
||
| 231 | curl_setopt($ch, $key, $value); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | if (isset($opt['proxy'])) { |
||
| 236 | curl_setopt($ch, CURLOPT_PROXY, $opt['proxy']); |
||
| 237 | curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); |
||
| 238 | if (isset($opt['proxy_type'])) { |
||
| 239 | switch ($opt['proxy_type']) { |
||
| 240 | case 'socks5': |
||
| 241 | curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); |
||
| 242 | break; |
||
| 243 | case 'http': |
||
| 244 | curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
||
| 245 | break; |
||
| 246 | case 'https': |
||
| 247 | curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS); |
||
| 248 | break; |
||
| 249 | case 'socks4': |
||
| 250 | curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4); |
||
| 251 | break; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | $data = curl_exec($ch); |
||
| 257 | if (!is_string($data) && $data) { |
||
| 258 | $data = json::assoc($data); |
||
| 259 | } |
||
| 260 | $result['curl_exec'] = $data; |
||
| 261 | // save to log |
||
| 262 | $parse_url = helper::parse_url2($opt['url']); |
||
| 263 | $filesave = "{$parse_url['host']}/{$parse_url['path']}"; |
||
| 264 | $filepath = helper::platformSlashes(__DIR__ . "/log/curl_exec/$msisdn/$filesave"); |
||
| 265 | if (!is_dir(dirname($filepath))) { |
||
| 266 | mkdir(dirname($filepath), 0777, true); |
||
| 267 | } |
||
| 268 | file::file($filepath, $data, true); |
||
| 269 | //rewind($curl_log); |
||
| 270 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
||
| 271 | $headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT); |
||
| 272 | $headerSent = explode("\n", $headerSent); |
||
| 273 | $headerSent = array_map(function ($v) { |
||
| 274 | return preg_replace("/\r$/m", '', $v); |
||
| 275 | }, $headerSent); |
||
| 276 | $result['request']['raw'] = $headerSent; |
||
| 277 | $header = substr($data, 0, $header_size); |
||
| 278 | $body = substr($data, $header_size); |
||
| 279 | if (json::is_json($body)) { |
||
| 280 | $body = json_decode($body, true); |
||
| 281 | } |
||
| 282 | |||
| 283 | $header = explode("\n", $header); |
||
| 284 | $header = array_map(function ($v) { |
||
| 285 | return preg_replace("/\r$/m", '', $v); |
||
| 286 | }, $header); |
||
| 287 | |||
| 288 | $result['response']['headers'] = $header; |
||
| 289 | foreach ($header as $h) { |
||
| 290 | $ex = explode(':', $h); |
||
| 291 | if (isset($ex[0]) && isset($ex[1])) { |
||
| 292 | $hkey = $ex[0]; |
||
| 293 | //var_dump($hkey, strpos(strtolower(trim($hkey)), 'oauth')); |
||
| 294 | if (false !== strpos(strtolower($hkey), 'oauth')) { |
||
| 295 | $_SESSION['im3'][$opt['url']]['oauth'] = trim($ex[1]); |
||
| 296 | $_SESSION['im3']['oauth'] = trim($ex[1]); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } |
||
| 300 | if (is_iterable($body)) { |
||
| 301 | foreach ($body as $key => $value) { |
||
| 302 | $_SESSION['im3'][$key] = $value; |
||
| 303 | } |
||
| 304 | if (isset($body['data']['tokenid'])) { |
||
| 305 | $_SESSION['im3']['tokenid'] = $body['data']['tokenid']; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | $result['response']['body'] = $body; |
||
| 309 | $result['options'] = $opt; |
||
| 310 | if (isset($opt['verbose'])) { |
||
| 311 | $_SESSION['verbose'][$opt['url']] = $result; |
||
| 312 | } |
||
| 313 | //exit(gettype($ch)); |
||
| 314 | curl_close($ch); |
||
| 315 | |||
| 316 | return $result; |
||
| 317 | } |
||
| 391 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.