48function getURLContent($url, $postorget =
'GET', $param =
'', $followlocation = 1, $addheaders = array(), $allowedschemes = array(
'http',
'https'), $localurl = 0, $ssl_verifypeer = -1, $timeoutconnect = 0, $timeoutresponse = 0, $otherCurlOptions = array())
57 dol_syslog(
"getURLContent postorget=".$postorget.
" URL=".$url);
59 dol_syslog(
"getURLContent postorget=".$postorget.
" URL=".$url.
" json_encode(param)=".json_encode($param), LOG_DEBUG, 0,
'_curl');
62 if (!function_exists(
'curl_init')) {
64 dol_syslog(
"getURLContent PHP curl library must be installed", LOG_DEBUG, 0,
'_curl');
66 return array(
'http_code' => 500,
'content' =>
'',
'curl_error_no' => 1,
'curl_error_msg' =>
'PHP curl library must be installed');
76 curl_setopt($ch, CURLOPT_VERBOSE,
true);
77 curl_setopt($ch, CURLOPT_USERAGENT,
'Dolibarr geturl function');
81 @curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
false);
83 if (is_array($addheaders) && count($addheaders)) {
84 curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders);
86 curl_setopt($ch, CURLINFO_HEADER_OUT,
true);
89 curl_setopt($ch, CURLOPT_HEADER,
true);
96 curl_setopt($ch, CURLOPT_SSLVERSION, (
int) $sslversion);
101 if ($ssl_verifypeer < 0) {
102 global $dolibarr_main_prod;
103 $ssl_verifypeer = ($dolibarr_main_prod ? true :
false);
110 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, ($ssl_verifypeer ?
true :
false));
116 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ($ssl_verifypeer ? 2 : 0));
120 $redir_list = array();
121 if (is_array($allowedschemes)) {
122 foreach ($allowedschemes as $allowedscheme) {
123 if ($allowedscheme ==
'http') {
124 $protocols |= CURLPROTO_HTTP;
125 $redir_list[
"HTTP"] = 1;
126 } elseif ($allowedscheme ==
'https') {
127 $protocols |= CURLPROTO_HTTPS;
128 $redir_list[
"HTTPS"] = 1;
129 } elseif ($allowedscheme ==
'ftp') {
130 $protocols |= CURLPROTO_FTP;
131 $redir_list[
"FTP"] = 1;
132 } elseif ($allowedscheme ==
'ftps') {
133 $protocols |= CURLPROTO_FTPS;
134 $redir_list[
"FTPS"] = 1;
139 $newtimeoutconnect = ($timeoutconnect ? $timeoutconnect :
getDolGlobalInt(
'MAIN_USE_CONNECT_TIMEOUT', 5));
140 $newtimeoutresponse = ($timeoutresponse ? $timeoutresponse :
getDolGlobalInt(
'MAIN_USE_RESPONSE_TIMEOUT', 30));
143 dol_syslog(
"getURLContent newtimeoutconnect=".$newtimeoutconnect.
" newtimeoutresponse=".$newtimeoutresponse, LOG_DEBUG, 0,
'_curl');
146 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $newtimeoutconnect);
147 curl_setopt($ch, CURLOPT_TIMEOUT, $newtimeoutresponse);
151 if ($maxsize && defined(
'CURLOPT_MAXFILESIZE_LARGE')) {
152 curl_setopt($ch, CURLOPT_MAXFILESIZE_LARGE, $maxsize * 1024);
154 if ($maxsize && defined(
'CURLOPT_MAXFILESIZE')) {
155 curl_setopt($ch, CURLOPT_MAXFILESIZE, $maxsize * 1024);
159 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
160 if ($postorget ==
'POST') {
161 curl_setopt($ch, CURLOPT_POST,
true);
162 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
163 } elseif ($postorget ==
'POSTALREADYFORMATED') {
164 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'POST');
165 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
166 } elseif ($postorget ==
'PUT') {
167 $array_param = array();
168 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PUT');
169 if (!is_array($param)) {
170 parse_str($param, $array_param);
172 dol_syslog(
"parameter param must be a string", LOG_WARNING);
173 $array_param = $param;
175 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));
176 } elseif ($postorget ==
'PUTALREADYFORMATED') {
177 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PUT');
178 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
179 } elseif ($postorget ==
'PATCH') {
180 $array_param = array();
181 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PATCH');
182 if (!is_array($param)) {
183 parse_str($param, $array_param);
185 dol_syslog(
"parameter param must be a string", LOG_WARNING);
186 $array_param = $param;
188 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));
189 } elseif ($postorget ==
'PATCHALREADYFORMATED') {
190 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PATCH');
191 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
192 } elseif ($postorget ==
'HEAD') {
193 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'HEAD');
194 curl_setopt($ch, CURLOPT_NOBODY,
true);
195 } elseif ($postorget ==
'DELETE') {
196 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'DELETE');
198 curl_setopt($ch, CURLOPT_POST,
false);
203 dol_syslog(
"getURLContent set proxy to ".$PROXY_HOST.
":".$PROXY_PORT.
" - ".$PROXY_USER.
":".$PROXY_PASS);
205 curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST.
":".$PROXY_PORT);
207 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER.
":".$PROXY_PASS);
211 if (is_array($otherCurlOptions)) {
212 foreach ($otherCurlOptions as $option => $value) {
213 curl_setopt($ch, $option, $value);
223 if ($maxRedirection < 1) {
225 dol_syslog(
"getURLContent http_code=400 Maximum number of redirections reached", LOG_DEBUG, 0,
'_curl');
227 return array(
'http_code' => 400,
'content' =>
'Maximum number of redirections reached',
'curl_error_no' => 1,
'curl_error_msg' =>
'Maximum number of redirections reached');
230 curl_setopt($ch, CURLOPT_URL, $newUrl);
233 $newUrlArray = parse_url($newUrl);
234 $hosttocheck = $newUrlArray[
'host'];
235 $hosttocheck = str_replace(array(
'[',
']'),
'', $hosttocheck);
238 if (in_array($hosttocheck, array(
'metadata.google.internal'))) {
239 $info[
'http_code'] = 400;
240 $info[
'content'] =
'Error bad hostname '.$hosttocheck.
' (Used by Google metadata). This value for hostname is not allowed.';
242 dol_syslog(
"getURLContent http_code=400 ".$info[
'content'], LOG_DEBUG, 0,
'_curl');
244 return array(
'http_code' => 400,
'content' => $info[
'content'],
'curl_error_no' => 1,
'curl_error_msg' => $info[
'content']);
248 if (in_array($hosttocheck, array(
'localhost',
'localhost.domain'))) {
249 $iptocheck =
'127.0.0.1';
250 } elseif (in_array($hosttocheck, array(
'ip6-localhost',
'ip6-loopback'))) {
254 if (function_exists(
'gethostbyname')) {
255 $iptocheck = gethostbyname($hosttocheck);
257 $iptocheck = $hosttocheck;
263 if (!filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
270 $info[
'http_code'] = 400;
271 $info[
'content'] = $tmpresult;
273 dol_syslog(
"getURLContent http_code=400 ".$info[
'content'], LOG_DEBUG, 0,
'_curl');
275 return array(
'http_code' => 400,
'content' => $tmpresult,
'curl_error_no' => 1,
'curl_error_msg' => $tmpresult);
281 if (defined(
'CURLOPT_CONNECT_TO')) {
282 $connect_to = array(sprintf(
"%s:%d:%s:%d", $newUrlArray[
'host'], empty($newUrlArray[
'port']) ?
'' : $newUrlArray[
'port'], $iptocheck, empty($newUrlArray[
'port']) ?
'' : $newUrlArray[
'port']));
285 curl_setopt($ch, CURLOPT_CONNECT_TO, $connect_to);
291 curl_setopt($ch, CURLOPT_PROTOCOLS, $protocols);
292 curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $protocols);
300 $response = curl_exec($ch);
302 $info = curl_getinfo($ch);
303 $http_code = $info[
'http_code'];
305 if ($followlocation && ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307)) {
306 $newUrl = $info[
'redirect_url'];
313 }
while ($http_code);
315 $request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
317 dol_syslog(
"getURLContent request without content body=".$request);
320 dol_syslog(
"getURLContent request without body=".$request, LOG_DEBUG, 0,
'_curl');
321 dol_syslog(
"getURLContent response=".$response, LOG_DEBUG, 0,
'_curl');
323 dol_syslog(
"getURLContent response size=".strlen($response));
326 if (curl_errno($ch)) {
329 $rep[
'content'] = (string) $response;
331 $rep[
'content'] =
'';
334 $rep[
'http_code'] = 0;
335 $rep[
'curl_error_no'] = curl_errno($ch);
336 $rep[
'curl_error_msg'] = curl_error($ch);
338 dol_syslog(
"getURLContent response array is ".implode(
',', $rep));
340 dol_syslog(
"getURLContent curl_error_no=".$rep[
'curl_error_no'].
" curl_error_msg=".$rep[
'curl_error_msg'], LOG_DEBUG, 0,
'_curl');
351 dol_syslog(
"getURLContent http_code=".$rep[
'http_code']);
355 $rep[
'content'] = (string) $response;
357 $rep[
'header'] = substr($rep[
'content'], 0, intval($rep[
'header_size']));
358 $rep[
'content'] = substr($rep[
'content'], intval($rep[
'header_size']));
361 $rep[
'content'] =
'';
364 $rep[
'curl_error_no'] = 0;
365 $rep[
'curl_error_msg'] =
'';