Composing cURL Request with PHP

Hi there. I feel like I’m missing something obvious here. My goal is to take a cURL command and be able to execute it using PHP. Not rocket science.

Here’s my cURL command that executes flawlessly:

curl -s -d \
'{
    "auth":
    {
       "RAX-KSKEY:apiKeyCredentials":
       {  
          "username": "username_here",  
          "apiKey": "apikey_here"}
    }  
}' \
-H 'Content-Type: application/json' \
'https://identity.api.rackspacecloud.com/v2.0/tokens' | python -m json.tool

Here’s my PHP that fails miserably:

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://identity.api.rackspacecloud.com/v2.0/tokens',
    CURLOPT_USERAGENT => 'cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
		json => $jsonData
    ),
    CURLOPT_CONNECTTIMEOUT => 60,
    CURLOPT_HTTPHEADER, array(
	    Content-Type => 'application/json'
	),
	CURLOPT_BINARYTRANSFER => 1
));
if(!curl_exec($curl)){
	die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

By the way, the value of $jsonData is:

{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"username_here","apiKey":"apikey_here"}}}

When I execute this PHP code, I receive an error back, code 415, unsupportedMediaType.

Any idea what I’m doing wrong here?

Thanks!

There is a few errors in the code that you pasted, but I am not sure if some is due to the forum has stripped some of the tags.

The postfields and header is wrong, should also not be binary transfer I guess. Though even when updating those I was not able to get it returning anything else using your code. Though I normally not use the curl_setopt_array, so changed the code to use normal curl_setopts and got it working.

Well, returning a different error message, mainly that the login credentials are wrong:
{“unauthorized”:{“code”:401,“message”:“Username or api key is invalid.”}}

[code]$data = ‘{“auth”:{“RAX-KSKEY:apiKeyCredentials”:{“username”:“username_here”,“apiKey”:“apikey_here”}}}’;
$url = ‘https://identity.api.rackspacecloud.com/v2.0/tokens’;

$handle = curl_init();

curl_setopt($handle, CURLOPT_URL, $url);

curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_VERBOSE, 1);
curl_setopt($handle, CURLOPT_HEADER, 0);
curl_setopt($handle, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’));
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($handle, CURLOPT_POST, 1);

curl_setopt($handle, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($handle);

if ($response === false) {
curl_close($handle);

return false;
}

var_dump($response);[/code]

Did you try a value of a REAL user agent. You can get it from the PHP variable: $_SERVER[‘HTTP_USER_AGENT’] or from the site ‘http://whatismyuseragent.com’.

An explanation about the error is found here.

That worked great, thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.