Skip to content

Commit 8f0cba0

Browse files
committed
Add Curl class for simple Curl requests
1 parent 494ccb5 commit 8f0cba0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/Libraries/Core/Curl.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)