[英]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 ) 我正在尝试使用Rackspace的“ Cloud Identity” API生成令牌( 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}} 我得到了: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';
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"}} 我收到以下错误代码:400 {“ badRequest”:{“ code”:400,“ message”:“无效的json请求正文”}}
Am I doing it right? 我做对了吗? Thanks in advance.
提前致谢。
Is there any reason why you're not using the PHP SDK ? 有什么理由不使用PHP SDK ? It makes doing things like this a lot easier.
这样可以很容易地做到这一点。
You'd need to change your PHP to: 您需要将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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.