51function getURLContent($url, $postorget =
'GET', $param =
'', $followlocation = 1, $addheaders = array(), $allowedschemes = array(
'http',
'https'), $localurl = 0, $ssl_verifypeer = -1, $timeoutconnect = 0, $timeoutresponse = 0, $otherCurlOptions = array(), $morelogsuffix =
'')
60 dol_syslog(
"getURLContent postorget=".$postorget.
" URL=".$url);
62 dol_syslog(
"getURLContent postorget=".$postorget.
" URL=".$url.
" json_encode(param)=".json_encode($param), LOG_DEBUG, 0,
'_curl');
65 dol_syslog(
"getURLContent postorget=".$postorget.
" URL=".$url.
" json_encode(param)=".json_encode($param), LOG_DEBUG, 0, $morelogsuffix);
68 if (!function_exists(
'curl_init')) {
70 dol_syslog(
"getURLContent PHP curl library must be installed", LOG_DEBUG, 0,
'_curl');
73 dol_syslog(
"getURLContent PHP curl library must be installed", LOG_DEBUG, 0, $morelogsuffix);
76 return array(
'http_code' => 500,
'content' =>
'',
'curl_error_no' => 1,
'curl_error_msg' =>
'PHP curl library must be installed');
86 curl_setopt($ch, CURLOPT_VERBOSE,
true);
87 curl_setopt($ch, CURLOPT_USERAGENT,
'Dolibarr geturl function');
91 @curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
false);
93 if (is_array($addheaders) && count($addheaders)) {
94 curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders);
96 curl_setopt($ch, CURLINFO_HEADER_OUT,
true);
99 curl_setopt($ch, CURLOPT_HEADER,
true);
106 curl_setopt($ch, CURLOPT_SSLVERSION, (
int) $sslversion);
111 if ($ssl_verifypeer < 0) {
112 global $dolibarr_main_prod;
113 $ssl_verifypeer = ($dolibarr_main_prod ? true :
false);
120 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, ($ssl_verifypeer ?
true :
false));
126 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ($ssl_verifypeer ? 2 : 0));
130 $redir_list = array();
131 if (is_array($allowedschemes)) {
132 foreach ($allowedschemes as $allowedscheme) {
133 if ($allowedscheme ==
'http') {
134 $protocols |= CURLPROTO_HTTP;
135 $redir_list[
"HTTP"] = 1;
136 } elseif ($allowedscheme ==
'https') {
137 $protocols |= CURLPROTO_HTTPS;
138 $redir_list[
"HTTPS"] = 1;
139 } elseif ($allowedscheme ==
'ftp') {
140 $protocols |= CURLPROTO_FTP;
141 $redir_list[
"FTP"] = 1;
142 } elseif ($allowedscheme ==
'ftps') {
143 $protocols |= CURLPROTO_FTPS;
144 $redir_list[
"FTPS"] = 1;
148 return array(
'http_code' => 500,
'content' =>
'',
'curl_error_no' => 1,
'curl_error_msg' =>
'Parameter allowedschemes of getURLContent must be an array of protocol schemes');
151 $newtimeoutconnect = ($timeoutconnect ? $timeoutconnect :
getDolGlobalInt(
'MAIN_USE_CONNECT_TIMEOUT', 5));
152 $newtimeoutresponse = ($timeoutresponse ? $timeoutresponse :
getDolGlobalInt(
'MAIN_USE_RESPONSE_TIMEOUT', 30));
155 dol_syslog(
"getURLContent newtimeoutconnect=".$newtimeoutconnect.
" newtimeoutresponse=".$newtimeoutresponse, LOG_DEBUG, 0,
'_curl');
157 if ($morelogsuffix) {
158 dol_syslog(
"getURLContent newtimeoutconnect=".$newtimeoutconnect.
" newtimeoutresponse=".$newtimeoutresponse, LOG_DEBUG, 0, $morelogsuffix);
161 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $newtimeoutconnect);
162 curl_setopt($ch, CURLOPT_TIMEOUT, $newtimeoutresponse);
166 if ($maxsize && defined(
'CURLOPT_MAXFILESIZE_LARGE')) {
167 curl_setopt($ch, CURLOPT_MAXFILESIZE_LARGE, $maxsize * 1024);
169 if ($maxsize && defined(
'CURLOPT_MAXFILESIZE')) {
170 curl_setopt($ch, CURLOPT_MAXFILESIZE, $maxsize * 1024);
174 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
175 if ($postorget ==
'POST') {
176 curl_setopt($ch, CURLOPT_POST,
true);
177 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
178 } elseif ($postorget ==
'POSTALREADYFORMATED') {
179 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'POST');
180 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
181 } elseif ($postorget ==
'PUT') {
182 $array_param = array();
183 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PUT');
184 if (!is_array($param)) {
185 parse_str($param, $array_param);
187 dol_syslog(
"parameter param must be a string", LOG_WARNING);
188 $array_param = $param;
190 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));
191 } elseif ($postorget ==
'PUTALREADYFORMATED') {
192 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PUT');
193 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
194 } elseif ($postorget ==
'PATCH') {
195 $array_param = array();
196 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PATCH');
197 if (!is_array($param)) {
198 parse_str($param, $array_param);
200 dol_syslog(
"parameter param must be a string", LOG_WARNING);
201 $array_param = $param;
203 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));
204 } elseif ($postorget ==
'PATCHALREADYFORMATED') {
205 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PATCH');
206 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
207 } elseif ($postorget ==
'HEAD') {
208 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'HEAD');
209 curl_setopt($ch, CURLOPT_NOBODY,
true);
210 curl_setopt($ch, CURLOPT_HEADER,
true);
211 } elseif ($postorget ==
'DELETE') {
212 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'DELETE');
214 curl_setopt($ch, CURLOPT_POST,
false);
219 dol_syslog(
"getURLContent set proxy to ".$PROXY_HOST.
":".$PROXY_PORT.
" - ".$PROXY_USER.
":".$PROXY_PASS);
221 curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST.
":".$PROXY_PORT);
223 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER.
":".$PROXY_PASS);
227 if (is_array($otherCurlOptions)) {
228 foreach ($otherCurlOptions as $option => $value) {
229 curl_setopt($ch, $option, $value);
239 if ($maxRedirection < 1) {
241 dol_syslog(
"getURLContent http_code=400 Maximum number of redirections reached", LOG_DEBUG, 0,
'_curl');
243 return array(
'http_code' => 400,
'content' =>
'Maximum number of redirections reached',
'curl_error_no' => 1,
'curl_error_msg' =>
'Maximum number of redirections reached');
246 curl_setopt($ch, CURLOPT_URL, $newUrl);
249 $newUrlArray = parse_url($newUrl);
250 $hosttocheck = $newUrlArray[
'host'] ?: $newUrlArray[
'path'];
251 $hosttocheck = str_replace(array(
'[',
']'),
'', $hosttocheck);
254 if (in_array($hosttocheck, array(
'metadata.google.internal'))) {
255 $info[
'http_code'] = 400;
256 $info[
'content'] =
'Error bad hostname '.$hosttocheck.
' (Used by Google metadata). This value for hostname is not allowed.';
258 dol_syslog(
"getURLContent http_code=400 ".$info[
'content'], LOG_DEBUG, 0,
'_curl');
260 return array(
'http_code' => 400,
'content' => $info[
'content'],
'curl_error_no' => 1,
'curl_error_msg' => $info[
'content']);
264 if (in_array($hosttocheck, array(
'localhost',
'localhost.domain'))) {
265 $iptocheck =
'127.0.0.1';
266 } elseif (in_array($hosttocheck, array(
'ip6-localhost',
'ip6-loopback'))) {
274 if (!filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
281 $info[
'http_code'] = 400;
282 $info[
'content'] = $tmpresult;
284 dol_syslog(
"getURLContent http_code=400 ".$info[
'content'], LOG_DEBUG, 0,
'_curl');
286 return array(
'http_code' => 400,
'content' => $tmpresult,
'curl_error_no' => 1,
'curl_error_msg' => $tmpresult);
292 if (defined(
'CURLOPT_CONNECT_TO')) {
293 $connect_to = array(sprintf(
"%s:%d:%s:%d", $newUrlArray[
'host'], empty($newUrlArray[
'port']) ?
'' : $newUrlArray[
'port'], $iptocheck, empty($newUrlArray[
'port']) ?
'' : $newUrlArray[
'port']));
296 curl_setopt($ch, CURLOPT_CONNECT_TO, $connect_to);
302 curl_setopt($ch, CURLOPT_PROTOCOLS, $protocols);
303 curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $protocols);
311 $response = curl_exec($ch);
313 $info = curl_getinfo($ch);
314 $http_code = $info[
'http_code'];
316 if ($followlocation && ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307)) {
317 $newUrl = $info[
'redirect_url'];
324 }
while ($http_code);
326 $request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
328 dol_syslog(
"getURLContent request without content body=".$request);
331 dol_syslog(
"getURLContent request without body=".$request, LOG_DEBUG, 0,
'_curl');
332 dol_syslog(
"getURLContent response=".$response, LOG_DEBUG, 0,
'_curl');
334 if ($morelogsuffix) {
336 dol_syslog(
"getURLContent request without body=".$request, LOG_DEBUG, 0, $morelogsuffix);
337 dol_syslog(
"getURLContent response=".$response, LOG_DEBUG, 0, $morelogsuffix);
340 dol_syslog(
"getURLContent response size=".strlen($response));
343 if (curl_errno($ch)) {
346 $rep[
'content'] = (
string) $response;
348 $rep[
'content'] =
'';
351 $rep[
'http_code'] = 0;
352 $rep[
'curl_error_no'] = curl_errno($ch);
353 $rep[
'curl_error_msg'] = curl_error($ch);
355 dol_syslog(
"getURLContent response array is ".implode(
',', $rep));
358 dol_syslog(
"getURLContent curl_error_no=".$rep[
'curl_error_no'].
" curl_error_msg=".$rep[
'curl_error_msg'], LOG_DEBUG, 0,
'_curl');
360 if ($morelogsuffix) {
361 dol_syslog(
"getURLContent curl_error_no=".$rep[
'curl_error_no'].
" curl_error_msg=".$rep[
'curl_error_msg'], LOG_DEBUG, 0, $morelogsuffix);
372 dol_syslog(
"getURLContent http_code=".$rep[
'http_code']);
376 $rep[
'content'] = (
string) $response;
377 if ($postorget ==
'HEAD' ||
getDolGlobalInt(
'MAIN_CURL_GET_RESPONSE_HEADER')) {
378 $rep[
'header'] = substr($rep[
'content'], 0, intval($rep[
'header_size']));
379 $rep[
'content'] = substr($rep[
'content'], intval($rep[
'header_size']));
382 $rep[
'content'] =
'';
385 $rep[
'curl_error_no'] = 0;
386 $rep[
'curl_error_msg'] =
'';