"BASIC", "username" => "your_username", "password" => "your_password") * * @return mixed */ function curl(string $method, string $url,$data_raw, array $headers_parameters, array $auth){ $curl = curl_init (); $headers = []; $data = ''; //on type le type de données if(isset($headers_parameters['Content-type'])){ switch ($headers_parameters['Content-type']){ case 'json': $headers[] = 'Content-type: application/json'; if(\is_array($data_raw)) $data = json_encode($data_raw); else $data = $data_raw; break; case 'xml' : $headers[] = 'Content-type: application/xml'; if(\is_array($data_raw)){ $xml = new SimpleXMLElement(''); array_walk_recursive($data_raw, array ($xml, 'addChild')); $data = $xml->asXML(); } else $data = $data_raw; break; case 'text' : $headers[] = 'Content-type: text/plain'; if(\is_array($data_raw)){ $data = \implode("\n",$data_raw); }else $data = $data_raw; break; default : $headers[] = 'Content-type: text/plain'; if(\is_array($data_raw)){ $data = \implode("\n",$data_raw); }else $data = $data_raw; break; } } else { $headers[] = 'Content-type: text/plain'; if(\is_array($data_raw)){ $data = \implode("\n",$data_raw); }else $data = $data_raw; } switch($method){ case "POST": curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); break; case "GET": curl_setopt($curl, CURLOPT_HTTPGET, true); $url = sprintf('%s?%s', $url, http_build_query($data_raw)); break; /* à completer si envoi de fichier CURLOPT_INFILE et CURLOPT_INFILESIZE case "PUT": curl_setopt($curl, CURLOPT_PUT, true); break; */ default: //les datas sont build dans l'url $url = sprintf('%s?%s', $url, http_build_query($data)); } //gestion de l'auth if(isset($auth['type'])){ $username = $auth['username']; $password = $auth['password']; switch($auth['type']){ case 'BASIC' : curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); break; case 'BASIC64' : $headers[] = 'Authorization: Basic '. base64_encode("$username:$password"); break; } } if(isset($headers_parameters['ssl']) && $headers_parameters['ssl'] === true) { curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1); } else { //attention pas sécurisé curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); } curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_FRESH_CONNECT, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_TIMEOUT, 300); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); if (! $result = curl_exec($curl)) { //si il y a une erreur $this->curl_error = utf8_decode(curl_error ( $curl )) ; curl_close($curl); return null; } curl_close($curl); return $result; } /** * @return string */ public function getCurlError() : string{ return $this->curl_error; } }