-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCURL.php
More file actions
executable file
·106 lines (105 loc) · 3.47 KB
/
CURL.php
File metadata and controls
executable file
·106 lines (105 loc) · 3.47 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace App\Api;
class CURL
{
public $address;
public $port = 443;
public function __construct($address)
{
$this->ch = curl_init($address);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookies.txt');
curl_setopt($this->ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookies.txt');
$this->address = $address;
}
public function addurl($extra_uri)
{
$this->address = $this->address . $extra_uri;
curl_setopt($this->ch, CURLOPT_URL, $this->address);
}
public function set_method($method, $params = [])
{
switch ($method)
{
case 'POST':
curl_setopt_array($this->ch, [CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query($params) ]);
break;
case 'GET':
curl_setopt_array($this->ch, [CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_URL => $this->address . '?' . self::convert_to_query($params) ]);
break;
case 'PUT':
curl_setopt_array($this->ch, [CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => http_build_query($params) ]);
break;
case 'DELETE':
curl_setopt_array($this->ch, [CURLOPT_CUSTOMREQUEST => 'DELETE', CURLOPT_POSTFIELDS => http_build_query($params) ]);
break;
}
}
public function set_header($headers)
{
curl_setopt($this->ch, CURLOPT_HTTPHEADER, self::convert_to_standardheader($headers));
}
public function set_port($port)
{
curl_setopt($this->ch, CURLOPT_PORT, $port);
}
public function set_timeout($timeout)
{
curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
}
public function set_proxy($address, $port, $type = 'CURLPROXY_HTTP')
{
curl_setopt($this->ch, CURLOPT_PROXY, $address . ':' . $port);
curl_setopt($this->curl, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($this->curl, CURLOPT_PROXYTYPE, $type);
}
private static function convert_to_query($params)
{
$num = 0;
$query = '';
$keys = array_keys($params);
$values = array_values($params);
foreach ($params as $parameter)
{
$query .= $keys[$num] . '=' . $values[$num] . '&&';
$num++;
}
}
private static function convert_to_standardheader($params)
{
$query = [];
$num = 0;
$keys = array_keys($params);
$values = array_values($params);
foreach ($params as $parameter)
{
array_push($query, $keys[$num] . ': ' . $values[$num]);
$num++;
}
return $query;
}
public function addopt($options)
{
if (is_array($options)) curl_setopt_array($this->ch, $options);
else curl_setopt($this->ch, array_keys($options) [0], array_values($options) [0]);
}
public function execute()
{
$this->response = curl_exec($this->ch);
$this->httpcode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
if (!empty(curl_errno($this->ch))):
$this->errorcode = curl_errno($this->ch);
$this->error = curl_error($this->ch);
file_put_contents("error.txt", $this->error);
endif;
}
public function close()
{
curl_close($this->ch);
}
public function __destruct()
{
curl_close($this->ch);
}
}