Accessing Kumulos APIs and Database from PHP

Not Just for iPhone

A lot of people are asking how to access Kumulos API methods from outside an iOS and OSX app, so today’s blog is about doing exactly that.

This is really easy to do from any system that supports HTTP POST requests and JSON – you can access Kumulos APIs from C#, ASP.NET, Android, Flash and more. In fact, Kumulos API methods currently support both Apple Plist and JSON output. Naturally Apple Plists are used when you access the API methods with the Objective-C bindings that Kumulos provides for your app but the JSON response is really useful for accessing API methods from anywhere else.

Basically accessing Kumulos from outside an iOS app is simply a case of building a POST  and posting it to http://api.kumulos.com/YOUR_API_KEY/METHOD_ALIAS.json.

The API key can be found on the application dashboard in Kumulos, the method alias is listed on the API Methods panel, under the heading ‘ALIAS’.

The Parameters

The necessary parameters for any API method are:

  • salt – a salt for hashing the secret key, can just be a random number
  • hashedKey – an MD5 hash of your applications secret key concatenated with the salt.

The method parameters are added as a POST array called: params[].

for example if your method has 2 parameters – ‘username’ and ‘password’ you would add the following parameters:

  • params[username]
  • params[password]
Some Example PHP Code

[code]
1
$params = array(
//’params[userID]’=>1
);

$salt = rand(1,9999999);
$ch = curl_init(‘http://api.kumulos.com/’.$apiKey.’/’.$APIMethodAlias.’.json’);
$hashedKey = md5($secretKey.$salt);

$params = array_merge(array (‘hashedKey’=>$hashedKey,’salt’=>$salt),$params);

$requestBody = ”;
foreach($params as $name => $value) {
$requestBody .= urlencode($name).’=’.urlencode($value).’&’;
}
// chop off last ampersand
$requestBody = substr($requestBody, 0, strlen($requestBody)-1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

$output = json_decode($response);

echo ‘

';
print_r($output);
echo '

‘;

?>
[/code]

If you have written some code in any other language, we’d love to hear about it! We’ll post the code alongside the PHP example to help other developers.