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 \
          "username": "username_here",  
          "apiKey": "apikey_here"}
}' \
-H 'Content-Type: application/json' \
'' | python -m json.tool

Here’s my PHP that fails miserably:

curl_setopt_array($curl, array(
    CURLOPT_URL => '',
    CURLOPT_USERAGENT => 'cURL Request',
    CURLOPT_POST => 1,
		json => $jsonData
	    Content-Type => 'application/json'
	die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));

By the way, the value of $jsonData is:


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

Any idea what I’m doing wrong here?


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 = ‘’;

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

return false;


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 ‘’.

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.