简体   繁体   中英

Curl PHP errors with rackspace api

I'm trying to generate a token with the "Cloud Identity" API from Rackspace( https://developer.rackspace.com/docs/cloud-identity/v2/developer-guide/#generate-an-authentication-token )

This is the request i need:

$ 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

And this is how I'm trying to do it:

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

And I get this: 415 {"unsupportedMediaType":{"code":415}}

When I change the header from

$headers[0] = '"Content-Type: application/json" | python -m json.tool'; 

to

$headers[0] = 'Content-Type: application/json | python -m json.tool';

I get this

And finally when I change the header to this one:

$headers[0] = 'Content-Type: application/json';

i get this error code: 400 {"badRequest":{"code":400,"message":"Invalid json request body"}}

Am I doing it right? Thanks in advance.

Is there any reason why you're not using the PHP SDK ? It makes doing things like this a lot easier.

You'd need to change your PHP to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM