-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCurl.php
73 lines (61 loc) · 2.1 KB
/
Curl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Craftgate\Util;
class Curl
{
const CONNECT_TIMEOUT = 10;
const READ_TIMEOUT = 150;
public static function get($url, array $headers)
{
return self::request($url, array(
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_CONNECTTIMEOUT => self::CONNECT_TIMEOUT,
CURLOPT_TIMEOUT => self::READ_TIMEOUT,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
));
}
public static function post($url, array $headers, $content)
{
return self::request($url, array(
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_CONNECTTIMEOUT => self::CONNECT_TIMEOUT,
CURLOPT_TIMEOUT => self::READ_TIMEOUT,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $content ? json_encode($content) : '',
));
}
public static function put($url, array $headers, $content)
{
return self::request($url, array(
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_CONNECTTIMEOUT => self::CONNECT_TIMEOUT,
CURLOPT_TIMEOUT => self::READ_TIMEOUT,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $content ? json_encode($content) : null,
));
}
public static function delete($url, array $headers)
{
return self::request($url, array(
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_CONNECTTIMEOUT => self::CONNECT_TIMEOUT,
CURLOPT_TIMEOUT => self::READ_TIMEOUT,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
));
}
private static function request($url, $options)
{
$request = curl_init($url);
curl_setopt_array($request, $options);
$response = curl_exec($request);
if ($response === false) {
throw new \Exception(curl_error($request), curl_errno($request));
}
curl_close($request);
unset($request); // PHP 8.0
return $response;
}
}