Example: Accessing JSON server via cURL

Last modified: October 29, 2008 - 18:04

The simplest way to access any service is through cURL. Accessing a service via the The JSON server is no different.

In the following examples the data that is passed to the JSON server depends entirely on which service you are trying to access and what fields are required by that service.
For specific examples see the documentation provided by the individual services.

Generally speaking however you can access a service via the JSON server at the command line by issuing the following command:
curl --data method=METHODNAME  http://YOURSITE/?q=services/json
You may add more --data entries as necessary, to pass in parameters.
Array parameters are handled with NAME[]:
curl --data method=METHODNAME  http://YOURSITE/?q=services/json

You may alternatively wish to use PHP's cURL functions

<?php
  $api_key
= '99999your9999api999keyd4eae7c3'; // Your API key.
 
$sessid = '9999your9999sess9999id'; // Your session ID.
 
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_POST, 1);
 
curl_setopt($ch, CURLOPT_URL, 'http://YOURSITE/services/json');

 
//prepare the field values being posted to the service
 
$data = array(
   
'method' => 'method.somemethod',
   
'api_key' => $api_key,
   
'sessid' => $sessid,
   
'arg1' => 'some value',
   
'arg2' => 'somevalue',
   
'argN'=>'another value',
  );
 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

 
//make the request
 
$result = curl_exec($ch);
?>

See php cURL documentation for complete usage of php cURL.

JSON server with user.login

skyredwang - July 9, 2009 - 15:25

$domain = 'yourdomain.com';
$timestamp = '1247085501798';
$nonce = 'abcdzzzf';
$api_key = 'ed1b9a373e5713b2fe8ceff478c93aef'; // Your API key.
$hash = hash_hmac('sha256', $timestamp .';'.$domain .';'. $nonce .';'.'user.login',$api_key );
echo 'HASH: '.$hash."\n"; // Debugging
 
  $sessid = '207ce01d2fb03a61b96cf629e99a1341'; // Your session ID.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_URL, 'http://yourdomain.com/services/json');

  //prepare the field values being posted to the service
  $data = array(
    'hash' => $hash,
    'domain_name' => $domain,
    'domain_time_stamp'=>$timestamp,
    'nonce'=>$nonce,
    'method' => 'user.login',
    'api_key' => $api_key,
    'sessid' => $sessid,
    'username' => 'skyredwang',
    'password' => 'great',
  );
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

  //make the request
  $result = curl_exec($ch);
  echo 'RESULT: '.print_r($result,1);  // Debugging

 
 

Drupal is a registered trademark of Dries Buytaert.