2015-11-12 7 views
1

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.

+0

Je voudrais configurer 'data' de $ comme un tableau, pas une chaîne. '$ data = array ('nom d'utilisateur' => 'XXXXXX')' – Chris

+0

finalement cela a fonctionné. Il semble que tout le problème était la chaîne de données $. { – PhilippeXXI

Répondre

0

Y a-t-il une raison pour laquelle vous n'utilisez pas le PHP SDK? Cela facilite beaucoup les choses comme ça.

Vous auriez besoin de changer PHP:

<?php 
$url = 'https://identity.api.rackspacecloud.com/v2.0/tokens'; 
$username = 'foo'; 
$apiKey = 'bar'; 

$data = <<<EOT 
{"auth": {"RAX-KSKEY:apiKeyCredentials":{"username":"$username","apiKey":"$apiKey"}}} 
EOT; 

$headers = array('Content-Type: application/json'); 

$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); 

echo curl_getinfo($ch, CURLINFO_HTTP_CODE); 
+0

bien, pas vraiment, je vais essayer avec le SDK PHP. Merci de votre aide – PhilippeXXI