42function getURLContent($url, $postorget =
'GET', $param =
'', $followlocation = 1, $addheaders = array(), $allowedschemes = array(
'http',
'https'), $localurl = 0, $ssl_verifypeer = -1)
52 dol_syslog(
"getURLContent postorget=".$postorget.
" URL=".$url.
" param=".$param);
54 if (!function_exists(
'curl_init')) {
55 return array(
'http_code' => 500,
'content' =>
'',
'curl_error_no' => 1,
'curl_error_msg' =>
'PHP curl library must be installed');
65 curl_setopt($ch, CURLOPT_VERBOSE, 1);
66 curl_setopt($ch, CURLOPT_USERAGENT,
'Dolibarr geturl function');
70 @curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
false);
72 if (is_array($addheaders) && count($addheaders)) {
73 curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders);
75 curl_setopt($ch, CURLINFO_HEADER_OUT,
true);
80 curl_setopt($ch, CURLOPT_SSLVERSION,
$conf->global->MAIN_CURL_SSLVERSION);
85 if ($ssl_verifypeer < 0) {
86 global $dolibarr_main_prod;
87 $ssl_verifypeer = ($dolibarr_main_prod ? true :
false);
94 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, ($ssl_verifypeer ?
true :
false));
95 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ($ssl_verifypeer ?
true :
false));
99 $redir_list = array();
100 if (is_array($allowedschemes)) {
101 foreach ($allowedschemes as $allowedscheme) {
102 if ($allowedscheme ==
'http') {
103 $protocols |= CURLPROTO_HTTP;
104 $redir_list[
"HTTP"] = 1;
105 } elseif ($allowedscheme ==
'https') {
106 $protocols |= CURLPROTO_HTTPS;
107 $redir_list[
"HTTPS"] = 1;
108 } elseif ($allowedscheme ==
'ftp') {
109 $protocols |= CURLPROTO_FTP;
110 $redir_list[
"FTP"] = 1;
111 } elseif ($allowedscheme ==
'ftps') {
112 $protocols |= CURLPROTO_FTPS;
113 $redir_list[
"FTPS"] = 1;
118 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,
getDolGlobalInt(
'MAIN_USE_CONNECT_TIMEOUT', 5));
119 curl_setopt($ch, CURLOPT_TIMEOUT,
getDolGlobalInt(
'MAIN_USE_RESPONSE_TIMEOUT', 30));
123 if ($maxsize && defined(
'CURLOPT_MAXFILESIZE_LARGE')) {
124 curl_setopt($ch, CURLOPT_MAXFILESIZE_LARGE, $maxsize);
126 if ($maxsize && defined(
'CURLOPT_MAXFILESIZE')) {
127 curl_setopt($ch, CURLOPT_MAXFILESIZE, $maxsize);
131 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
132 if ($postorget ==
'POST') {
133 curl_setopt($ch, CURLOPT_POST, 1);
134 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
135 } elseif ($postorget ==
'POSTALREADYFORMATED') {
136 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'POST');
137 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
138 } elseif ($postorget ==
'PUT') {
140 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PUT');
141 if (!is_array($param)) {
142 parse_str($param, $array_param);
144 dol_syslog(
"parameter param must be a string", LOG_WARNING);
145 $array_param = $param;
147 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));
148 } elseif ($postorget ==
'PUTALREADYFORMATED') {
149 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PUT');
150 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
151 } elseif ($postorget ==
'HEAD') {
152 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'HEAD');
153 curl_setopt($ch, CURLOPT_NOBODY,
true);
154 } elseif ($postorget ==
'DELETE') {
155 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'DELETE');
157 curl_setopt($ch, CURLOPT_POST, 0);
162 dol_syslog(
"getURLContent set proxy to ".$PROXY_HOST.
":".$PROXY_PORT.
" - ".$PROXY_USER.
":".$PROXY_PASS);
164 curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST.
":".$PROXY_PORT);
166 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER.
":".$PROXY_PASS);
176 if ($maxRedirection < 1) {
180 curl_setopt($ch, CURLOPT_URL, $newUrl);
183 $newUrlArray = parse_url($newUrl);
184 $hosttocheck = $newUrlArray[
'host'];
185 $hosttocheck = str_replace(array(
'[',
']'),
'', $hosttocheck);
188 if (in_array($hosttocheck, array(
'metadata.google.internal'))) {
189 $info[
'http_code'] = 400;
190 $info[
'content'] =
'Error bad hostname '.$hosttocheck.
' (Used by Google metadata). This value for hostname is not allowed.';
195 if (in_array($hosttocheck, array(
'localhost',
'localhost.domain'))) {
196 $iptocheck =
'127.0.0.1';
197 } elseif (in_array($hosttocheck, array(
'ip6-localhost',
'ip6-loopback'))) {
201 if (function_exists(
'gethostbyname')) {
202 $iptocheck = gethostbyname($hosttocheck);
204 $iptocheck = $hosttocheck;
210 if (!filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
217 $info[
'http_code'] = 400;
218 $info[
'content'] = $tmpresult;
225 if (defined(
'CURLOPT_CONNECT_TO')) {
226 $connect_to = array(sprintf(
"%s:%d:%s:%d", $newUrlArray[
'host'], empty($newUrlArray[
'port']) ?
'' : $newUrlArray[
'port'], $iptocheck, empty($newUrlArray[
'port']) ?
'' : $newUrlArray[
'port']));
229 curl_setopt($ch, CURLOPT_CONNECT_TO, $connect_to);
235 curl_setopt($ch, CURLOPT_PROTOCOLS, $protocols);
236 curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $protocols);
244 $response = curl_exec($ch);
246 $info = curl_getinfo($ch);
247 $http_code = $info[
'http_code'];
249 if ($followlocation && ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307)) {
250 $newUrl = $info[
'redirect_url'];
257 }
while ($http_code);
259 $request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
261 dol_syslog(
"getURLContent request=".$request);
264 dol_syslog(
"getURLContent request=".$request, LOG_DEBUG, 0,
'_curl');
265 dol_syslog(
"getURLContent response =".$response, LOG_DEBUG, 0,
'_curl');
267 dol_syslog(
"getURLContent response size=".strlen($response));
270 if (curl_errno($ch)) {
272 $rep[
'content'] = $response;
275 $rep[
'curl_error_no'] = curl_errno($ch);
276 $rep[
'curl_error_msg'] = curl_error($ch);
278 dol_syslog(
"getURLContent response array is ".implode(
',', $rep));
286 dol_syslog(
"getURLContent http_code=".$rep[
'http_code']);
290 $rep[
'content'] = $response;
292 $rep[
'curl_error_no'] =
'';
293 $rep[
'curl_error_msg'] =
'';