Créer des requêtes cURL simplement et rapidement
Par Nouvelle-Techno.fr le 23 août 2021 - Catégories : PHP
Lire l'article sur le site d'origine
cURL c'est quoi ?
Selon Wikipédia cURL (abréviation de client URL request library : « bibliothèque de requêtes aux URL pour les clients » ou see URL : « voir URL ») est une interface en ligne de commande, destinée à récupérer le contenu d'une ressource accessible par un réseau informatique.
Cette bibliothèque permet avec php de faire différentes requêtes avec les méthodes traditionnelles “Get” et “Post” sur des serveurs web, pratique pour la communication avec une api !
Dans certains projets nécessitant l'utilisation de CURL pour par exemple appelé une API avec PHP, recopié la requête à chaque fois, c'est long et assez pénible non ? Voici donc deux fonctions pour vous permettre d'être plus rapide :
1. Une fonction cURL avec la méthode "Get" sans token
function cURLGet($server, $calltype)
{
$ch = curl_init($server . $calltype);
curl_setopt_array($ch, array(
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Content-Type:application/json' // "Content-Type:application/json", et non pas "Content-Type: application/json"
),
));
$response = curl_exec($ch);
if ($response === false) {
return (curl_error($ch));
}
$responseData = json_decode($response, true);
return $responseData;
}
$server correspond à l'URL de votre api, par exemple : https://mon-api.com
$calltype correspond au chemin de votre api, par exemple : /v1/login/
Pour utiliser la fonction, un simple appel dans une variable vous suffit :
$maVariable = apiCallGet('https://mon-api.com', '/v1/login/');
2. Une fonction cURL avec la méthode "Get" avec token
function cURLGet($server, $access_token, $calltype)
{
$ch = curl_init($server . $calltype);
curl_setopt_array($ch, array(
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'authorization:Bearer ' . $access_token, // "authorization:Bearer", et non pas "authorization: Bearer"
'Content-Type:application/json' // "Content-Type:application/json", et non pas "Content-Type: application/json"
),
));
$response = curl_exec($ch);
if ($response === false) {
return (curl_error($ch));
}
$responseData = json_decode($response, true);
return $responseData;
}
La fonction fonctionne comme l'étape 1, la seule différence ici, nous avons la variable "$access_token".
$access_token correspond au token d'authentification API, "authorization:Bearer" lui correspond à une authentification de type Bearer.
L'utilisation de la fonction fonctionne comme l'étape 1, il vous suffit juste d'y ajouter votre token avec le $calltype.
3. Une fonction cURL avec la méthode "Post" sans token
function cURLPost($server, $postData, $calltype)
{
$ch = curl_init($server . $calltype);
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Content-Type:application/json' // "Content-Type:application/json", et non pas "Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
if ($response === false) {
return (curl_error($ch));
}
$responseData = json_decode($response, true);
return $responseData;
}
$server et $calltype sont identique que le point 1.
$postData est un tableau contenant vos paramètres à envoyer.
Exemple d'utilisation:
$dataPost = [
'grant_type' => 'password',
'username' => 'monUsername,
'password' => 'monPassword'
];
$postCurl= cURLPost('https://mon-api.com/, $dataPost, '/api/oauth/token');
4. Une fonction cURL avec la méthode "Post" avec token
function cURLPost($server, $postData, $access_token, $calltype)
{
$ch = curl_init($server . $calltype);
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'authorization:Bearer ' . $access_token, // "authorization:Bearer", et non pas "authorization: Bearer"
'Content-Type:application/json' // "Content-Type:application/json", et non pas "Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
$response = curl_exec($ch);
if ($response === false) {
return (curl_error($ch));
}
$responseData = json_decode($response, true);
return $responseData;
}
$server, $access_token et $calltype correspondent à l'étape 1 & 2, $postData à l'étape 3.
Avec ces fonctions, vous aurez de quoi faire des appels CURL rapidement et efficacement !