|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Libraries\Core; |
| 4 | + |
| 5 | +class Curl |
| 6 | +{ |
| 7 | + public static function execute(string $url, ?array $post_content = null, |
| 8 | + string $content_type = '', int $connect_timeout = 5, int $max_redirects = 10): array |
| 9 | + { |
| 10 | + $curl = \curl_init(); |
| 11 | + $time = \microtime(true); |
| 12 | + |
| 13 | + \curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $connect_timeout); |
| 14 | + |
| 15 | + \curl_setopt($curl, CURLOPT_AUTOREFERER, true); |
| 16 | + \curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
| 17 | + \curl_setopt($curl, CURLOPT_MAXREDIRS, $max_redirects); |
| 18 | + \curl_setopt($curl, CURLOPT_POSTREDIR, 7); |
| 19 | + |
| 20 | + \curl_setopt($curl, CURLOPT_URL, $url); |
| 21 | + |
| 22 | + if ($post_content) |
| 23 | + { |
| 24 | + \curl_setopt($curl, CURLOPT_POST, true); |
| 25 | + \curl_setopt($curl, CURLOPT_POSTFIELDS, $post_content); |
| 26 | + if (!empty($content_type)) |
| 27 | + { |
| 28 | + \curl_setopt($curl, CURLOPT_HTTPHEADER, [ |
| 29 | + 'Content-Type: ' . $content_type |
| 30 | + ]); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + \curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
| 35 | + |
| 36 | + $response = []; |
| 37 | + $response['data'] = \curl_exec($curl); |
| 38 | + $response['code'] = \curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 39 | + $response['type'] = \curl_getinfo($curl, CURLINFO_CONTENT_TYPE); |
| 40 | + $response['time'] = \microtime(true) - $time; |
| 41 | + |
| 42 | + \curl_close($curl); |
| 43 | + return $response; |
| 44 | + } |
| 45 | +} |
0 commit comments