45function getURLContent($url, $postorget =
'GET', $param =
'', $followlocation = 1, $addheaders = array(), $allowedschemes = array(
'http',
'https'), $localurl = 0, $ssl_verifypeer = -1, $timeoutconnect = 0, $timeoutresponse = 0)
55 dol_syslog(
"getURLContent postorget=".$postorget.
" URL=".$url.
" json_encode(param)=".json_encode($param));
57 if (!function_exists(
'curl_init')) {
58 return array(
'http_code' => 500,
'content' =>
'',
'curl_error_no' => 1,
'curl_error_msg' =>
'PHP curl library must be installed');
68 curl_setopt($ch, CURLOPT_VERBOSE, 1);
69 curl_setopt($ch, CURLOPT_USERAGENT,
'Dolibarr geturl function');
73 @curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
false);
75 if (is_array($addheaders) && count($addheaders)) {
76 curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders);
78 curl_setopt($ch, CURLINFO_HEADER_OUT,
true);
84 curl_setopt($ch, CURLOPT_SSLVERSION, (
int) $sslversion);
89 if ($ssl_verifypeer < 0) {
90 global $dolibarr_main_prod;
91 $ssl_verifypeer = ($dolibarr_main_prod ? true :
false);
98 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, ($ssl_verifypeer ?
true :
false));
99 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ($ssl_verifypeer ?
true :
false));
103 $redir_list = array();
104 if (is_array($allowedschemes)) {
105 foreach ($allowedschemes as $allowedscheme) {
106 if ($allowedscheme ==
'http') {
107 $protocols |= CURLPROTO_HTTP;
108 $redir_list[
"HTTP"] = 1;
109 } elseif ($allowedscheme ==
'https') {
110 $protocols |= CURLPROTO_HTTPS;
111 $redir_list[
"HTTPS"] = 1;
112 } elseif ($allowedscheme ==
'ftp') {
113 $protocols |= CURLPROTO_FTP;
114 $redir_list[
"FTP"] = 1;
115 } elseif ($allowedscheme ==
'ftps') {
116 $protocols |= CURLPROTO_FTPS;
117 $redir_list[
"FTPS"] = 1;
122 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeoutconnect ? $timeoutconnect :
getDolGlobalInt(
'MAIN_USE_CONNECT_TIMEOUT', 5));
123 curl_setopt($ch, CURLOPT_TIMEOUT, $timeoutresponse ? $timeoutresponse :
getDolGlobalInt(
'MAIN_USE_RESPONSE_TIMEOUT', 30));
127 if ($maxsize && defined(
'CURLOPT_MAXFILESIZE_LARGE')) {
128 curl_setopt($ch, CURLOPT_MAXFILESIZE_LARGE, $maxsize * 1024);
130 if ($maxsize && defined(
'CURLOPT_MAXFILESIZE')) {
131 curl_setopt($ch, CURLOPT_MAXFILESIZE, $maxsize * 1024);
135 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
136 if ($postorget ==
'POST') {
137 curl_setopt($ch, CURLOPT_POST, 1);
138 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
139 } elseif ($postorget ==
'POSTALREADYFORMATED') {
140 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'POST');
141 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
142 } elseif ($postorget ==
'PUT') {
144 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PUT');
145 if (!is_array($param)) {
146 parse_str($param, $array_param);
148 dol_syslog(
"parameter param must be a string", LOG_WARNING);
149 $array_param = $param;
151 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));
152 } elseif ($postorget ==
'PUTALREADYFORMATED') {
153 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PUT');
154 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
155 } elseif ($postorget ==
'PATCH') {
157 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PATCH');
158 if (!is_array($param)) {
159 parse_str($param, $array_param);
161 dol_syslog(
"parameter param must be a string", LOG_WARNING);
162 $array_param = $param;
164 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));
165 } elseif ($postorget ==
'PATCHALREADYFORMATED') {
166 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'PATCH');
167 curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
168 } elseif ($postorget ==
'HEAD') {
169 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'HEAD');
170 curl_setopt($ch, CURLOPT_NOBODY,
true);
171 } elseif ($postorget ==
'DELETE') {
172 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,
'DELETE');
174 curl_setopt($ch, CURLOPT_POST, 0);
179 dol_syslog(
"getURLContent set proxy to ".$PROXY_HOST.
":".$PROXY_PORT.
" - ".$PROXY_USER.
":".$PROXY_PASS);
181 curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST.
":".$PROXY_PORT);
183 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER.
":".$PROXY_PASS);
193 if ($maxRedirection < 1) {
194 return array(
'http_code' => 400,
'content' =>
'Maximum number of redirections reached',
'curl_error_no' => 1,
'curl_error_msg' =>
'Maximum number of redirections reached');
197 curl_setopt($ch, CURLOPT_URL, $newUrl);
200 $newUrlArray = parse_url($newUrl);
201 $hosttocheck = $newUrlArray[
'host'];
202 $hosttocheck = str_replace(array(
'[',
']'),
'', $hosttocheck);
205 if (in_array($hosttocheck, array(
'metadata.google.internal'))) {
206 $info[
'http_code'] = 400;
207 $info[
'content'] =
'Error bad hostname '.$hosttocheck.
' (Used by Google metadata). This value for hostname is not allowed.';
208 return array(
'http_code' => 400,
'content' => $info[
'content'],
'curl_error_no' => 1,
'curl_error_msg' => $info[
'content']);
212 if (in_array($hosttocheck, array(
'localhost',
'localhost.domain'))) {
213 $iptocheck =
'127.0.0.1';
214 } elseif (in_array($hosttocheck, array(
'ip6-localhost',
'ip6-loopback'))) {
218 if (function_exists(
'gethostbyname')) {
219 $iptocheck = gethostbyname($hosttocheck);
221 $iptocheck = $hosttocheck;
227 if (!filter_var($iptocheck, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
234 $info[
'http_code'] = 400;
235 $info[
'content'] = $tmpresult;
236 return array(
'http_code' => 400,
'content' => $tmpresult,
'curl_error_no' => 1,
'curl_error_msg' => $tmpresult);
242 if (defined(
'CURLOPT_CONNECT_TO')) {
243 $connect_to = array(sprintf(
"%s:%d:%s:%d", $newUrlArray[
'host'], empty($newUrlArray[
'port']) ?
'' : $newUrlArray[
'port'], $iptocheck, empty($newUrlArray[
'port']) ?
'' : $newUrlArray[
'port']));
246 curl_setopt($ch, CURLOPT_CONNECT_TO, $connect_to);
252 curl_setopt($ch, CURLOPT_PROTOCOLS, $protocols);
253 curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, $protocols);
261 $response = curl_exec($ch);
263 $info = curl_getinfo($ch);
264 $http_code = $info[
'http_code'];
266 if ($followlocation && ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307)) {
267 $newUrl = $info[
'redirect_url'];
274 }
while ($http_code);
276 $request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
278 dol_syslog(
"getURLContent request=".$request);
281 dol_syslog(
"getURLContent request=".$request, LOG_DEBUG, 0,
'_curl');
282 dol_syslog(
"getURLContent response =".$response, LOG_DEBUG, 0,
'_curl');
284 dol_syslog(
"getURLContent response size=".strlen($response));
287 if (curl_errno($ch)) {
290 $rep[
'content'] = (string) $response;
292 $rep[
'content'] =
'';
295 $rep[
'http_code'] = 0;
296 $rep[
'curl_error_no'] = curl_errno($ch);
297 $rep[
'curl_error_msg'] = curl_error($ch);
299 dol_syslog(
"getURLContent response array is ".implode(
',', $rep));
309 dol_syslog(
"getURLContent http_code=".$rep[
'http_code']);
313 $rep[
'content'] = (string) $response;
315 $rep[
'content'] =
'';
318 $rep[
'curl_error_no'] = 0;
319 $rep[
'curl_error_msg'] =
'';