Utiliser une api

Par Fwanky33, il y a 8 ans


Coucou à toutes et à tous,

J'ai besoin d'utiliser un webservice, on ma donné des éléments mais je suis incapable de mes utiliser. J'imagine pourtant que c'est ultra simple.

J'espère que quelqu'un pourra m'aider. Merci beaucoup.

Mon souhait est juste d eme connecter à l'api et d'ajouter un prospect comme ils me le préconisent.

Voici ce que j'ai


Une url tu type api.lesite.com

Ensuite, voici les éléments :

Le corps des requêtes doit être au format JSON.

Authentification

Pour interroger l'API, il nécessaire d'avoir un token.
Ce token est obtenable en appelant le webservice /api/v1/login/check en POST avec les paramètres :

{
"_username": "email",
"_password": "mdp"
}
Puis pour chaque requête, envoyer le token dans le header "Authorization" avec comme valeur "Bearer <inserer_token>".
Chaque token a une durée de validité de 24h.

Appel du webservice d'ajout de prospect

Ensuite, vous pourrez ajouter des prospects via le webservice /api/v1/customers/client en PUT avec les paramètres :

{
"country": {
"country_code": Code ISO 3166-1 alpha-2 du pays du prospect
},
"gender": Genre du prospect ("Monsieur" ou "Madame"),
"lastname": Nom du prospect,
"firstname": Prénom du prospect,
"email": Email du prospect,
"gclid": Google Click ID
}

1 réponse

yanis-git, il y a 8 ans

Voici le workflow en Curl ligne de commande.

# Method POST avec comme contentType JSON, utile pour récuperer ton token chaque 24h curl -H "Content-Type: application/json" -X POST -d '{"_username":"xyz","_password":"xyz"}' https://api.lesite.com/api/v1/login/check #Method PUT avec les headers JSON et authorization (remplace <mytoken> par le token récupéré avant). remplace le -d '{}' par ton json. curl -H "Content-Type: application/json" -H "Authorization: Bearer <MYTOKEN>" -X PUT -d '{your:json}' https://api.lesite.com//api/v1/customers/client

un php de php :

$data = array("name" => "Hagrid", "age" => "36"); $myToken = 'kfozekfop'; $data_string = json_encode($data); $ch = curl_init('http://api.local/rest/users'); //init url curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // post ou put curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // ton json curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', //Ton header json 'Content-Length: ' . strlen($data_string)), 'Authorization: Bearer'.$myToken // ton token si tu as besoin. ); $result = curl_exec($ch);