Je suis en train de générer un jeton avec le "Cloud Identity" API de Rackspace (https://developer.rackspace.com/docs/cloud-identity/v2/developer-guide/#generate-an-authentication-token)Curl erreurs PHP avec Rackspace api
C'est la demande que j'ai besoin:
$ curl https://identity.api.rackspacecloud.com/v2.0/tokens \
-X POST \
-d '{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"yourUserName","apiKey":"yourPassword"}}}' \
-H "Content-type: application/json" | python -m json.tool
Et cela est comment je suis en train de le faire:
<?php
$url = 'https://identity.api.rackspacecloud.com/v2.0/tokens';
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$data = "'{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username\":\"XXXXXXXX\",\"apiKey\":\"$apiKey\"}}}'";
$headers = array();
$headers[0] = '"Content-Type: application/json" | python -m json.tool';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
$response = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo($info);
?>
et je reçois ceci: 415 { "unsupportedMediaType": { "code": 415}}
Quand je change l'en-tête de
$headers[0] = '"Content-Type: application/json" | python -m json.tool';
à
$headers[0] = 'Content-Type: application/json | python -m json.tool';
Je reçois this
Et enfin quand je change de l'en-tête de celui-ci:
$headers[0] = 'Content-Type: application/json';
je reçois ce code d'erreur: 400 {"badRequest": {"code": 400, "message": "Corps de requête json invalide" }}
Est-ce que je le fais correctement? Merci d'avance.
Je voudrais configurer 'data' de $ comme un tableau, pas une chaîne. '$ data = array ('nom d'utilisateur' => 'XXXXXX')' – Chris
finalement cela a fonctionné. Il semble que tout le problème était la chaîne de données $. { – PhilippeXXI