-
Notifications
You must be signed in to change notification settings - Fork 1
/
curl-call.php
111 lines (85 loc) · 3.06 KB
/
curl-call.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
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
107
108
109
110
111
<?php
function curl_call($options = null)
{
static $ch_singleton;
if (!$options['return_handle'])
{
if (!$ch_singleton)
$ch_singleton = curl_init();
$ch = $ch_singleton;
} else
{
// When we "return_handle" this, we always need the fresh handler
$ch = curl_init();
}
$headers = array();
if (is_array($options['additional_request_headers']))
foreach ($options['additional_request_headers'] as $h)
$headers [] = $h;
curl_setopt($ch, CURLOPT_URL, $options['url']);
// Legacy-Untersttzung
if (!isset($options['max_time']) && isset($options['timeout']))
$options['max_time'] = $options['timeout'];
if (isset($options['max_time']))
curl_setopt($ch, CURLOPT_TIMEOUT, $options['max_time']);
else
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // Default-Timeout: 5 Sekunden
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Redirects erlauben
// Nur wenn open_basedir nicht gesetzt ist, denn sonst wrde cURL es ohnehin verhindern, als Schutz vor "file://"-URLS
if (!ini_get('open_basedir'))
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ($options['ignore_bad_certificates'] ? 0 : 2));
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
if ($options['username'] != '' || $options['password'] != '')
curl_setopt($ch, CURLOPT_USERPWD, $options['username'] . ':' . $options['password']);
if (isset($options['cookies']))
{
if (is_array($options['cookies']))
{
$cookies = '';
foreach ($options['cookies'] as $key => $val)
$cookies .= urlencode($key) . '=' . urlencode($val) . '; ';
$cookies = substr($cookies, 0, -2);
} else
$cookies = $options['cookies'];
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
}
// Wegen OpenSSL-Bug #3038 kann sich cURL mit OpenSSL 0.9.8 im TLS-Modus nicht zu bestimmten Servern verbinden,
// daher stellen wir fest den SSL3-Modus ein:
$vi = curl_version();
if (substr($vi['ssl_version'], 0, 13) == 'OpenSSL/0.9.8')
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
if (isset($options['postfields']))
{
if (is_array($options['postfields']) && $options['no_multipart'])
{
$postfields = '';
foreach ($options['postfields'] as $p => $v)
$postfields .= urlencode($p) . '=' . urlencode($v) . '&';
$postfields = substr($postfields, 0, -1);
} else
$postfields = $options['postfields'];
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
}
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($options['return_handle'])
return $ch;
curl_setopt($ch, CURLOPT_HEADER, !!$options['header_only']);
curl_setopt($ch, CURLOPT_NOBODY, !!$options['header_only']);
$result = curl_exec($ch);
if (isset($options['header_only']))
$result = new dfStringreturner($result, array('http_status' => curl_getinfo($ch, CURLINFO_HTTP_CODE)));
return $result;
}
function curl_post($url, $postfields = null, $options = null)
{
$options['url'] = $url;
$options['postfields'] = $postfields;
return curl_call($options);
}