Last updated February 4, 2010. Created by Amitaibu on September 9, 2008.
Edited by Roger Saner, andremolnar. Log in to edit this page.
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://example.com/?q=services/jsonYou may add more
--data entries as necessary, to pass in arguments, like so:curl --data 'method="my_module.connect"' --data 'name="Dries"' --data 'mail="me@example.com"' http://example.com/?q=services/jsonArray arguments are handled with NAME[]:
curl --data method=METHODNAME http://YOURSITE/?q=services/jsonYou 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);
?>If you are using API key authentication then use this:
<?php
$method = 'method.somemethod';
$api_key = '99999your9999api999keyd4eae7c3'; // Your API key.
$sessid = '9999your9999sess9999id'; // Your session ID.
$domain = $_SERVER['HTTP_HOST'];
$timestamp = (string) time();
$nonce = user_password();
$hash = hash_hmac('sha256', $timestamp .';'.$domain .';'. $nonce .';'. $method, $api_key);
$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 JSON service (WITH key authentication)
$data = array(
'method' => '"'. $method .'"',
'hash' => '"'. $hash .'"',
'domain_name' => '"'. $domain .'"',
'domain_time_stamp' => '"'. $timestamp .'"',
'nonce' => '"'. $nonce .'"',
//'sessid' => '"'. $sessid .'"', If you're using sessid, uncomment this line
'arg1' => '"some value"',
'arg1' => '"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.
Comments
JSON server with user.login
$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
hmm having issues with the code above returning token expired
here is the code, I had to make some adjustments for the php version im using 5.3x.. but here is the code im using its pretty much the same minus the '"something"' in the array
<?php
$domain = 'web.aqua';
$timestamp = '1247085501798';
$nonce = 'abcdz33zzf1';
$api_key = '0ae47d19ea111d843fca1e7446f76b39'; // 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://web.aqua/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' => '"test"',
'password' => '"test"',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//make the request
$result = curl_exec($ch);
echo 'JSON RESULT: ' .print_r($result,1); // debugging
?>
when I visit the page I get this data printed to the screen:
HASH: 6c29c1adac31787e5c2b53a6120895d791e6630caa0c60840dc1473383c98870
JSON RESULT: { "#error": false, "#data": { "#error": true, "#message": { "#error": true, "#message": "Token has expired." } } }
My key is correct, I checked it a few times, also token is set to expire after 40 seconds.
Any ideas?
Token has expired
Me too. Subscribing.
http://chalcedony.co.nz -- Building rock solid websites --
Bug in array setting
The array elements set with '"$var"' are being set to literally "$var" because they're single-quoted strings.
Ok, I was able to get user
Ok, I was able to get user information with user.get and this code below works for the latest php and 6.2x version of services with json.
<?php
// set the working directory to your Drupal root used for user_password();
chdir('/home/netcom/public_html/');
// require the bootstrap include
require_once './includes/bootstrap.inc';
// Load Drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$method = 'user.get';
$api_key = 'c5fa2adf2c7c04a66afbdsf8f2430e1'; // Your API key.
$sessid = '9999your999dsess999c'; // Your session ID.
$domain = $_SERVER['HTTP_HOST'];
$timestamp = (string) time();
$nonce = user_password();
$hash = hash_hmac('sha256', $timestamp .';'.$domain .';'. $nonce .';'. $method, $api_key);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, 'http://yoursite.com/services/json');
//prepare the field values being posted to the JSON service (WITH key authentication)
$data = array(
'method' => '"'. $method .'"',
'hash' => '"'. $hash .'"',
'domain_name' => '"'. $domain .'"',
'domain_time_stamp' => '"'. $timestamp .'"',
'nonce' => '"'. $nonce .'"',
'sessid' => '"'. $sessid .'"',
'uid' => '"1"',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//make the request
$result = curl_exec($ch);
echo print_r($result,1); // debugging
?>
Available Methods?
I can connect it seems, i get an internal prompt >
but i dont know what to do now. Is there a collection of available methods. Can i return a JSON-ified node, create a node, etc?
i always get this after i type anything:
301 Moved Permanently
Moved Permanently
The document has moved here.
I got the 301 until I
I got the 301 until I remembered to switched to www.example.com/services/json instead of example.com/services/json.
Services comes with a pile of built-in methods. Activate the sub-module for "node_services", for example. Or build your own!
API Key without Session: Isn't working.
Hi,
I need give access for a CakePHP App to the method "user.get", I have rewritten the user.get method to fetch the User by e-mail (ap_user.get).
The code is working very well using Session, however my CakePHP App don't have the Drupal Session info, only the API-Key for that I need to remove the "$sid" reference from my code:
<?php
global $user;
$webservice = 'http://localhost/services/json';
$api_key = '48afc1w3q3539000c6b14ec9fx3177ae';
$domain = $_SERVER['HTTP_HOST'];
$timestamp = (string) time();
$nonce = user_password();
$sid = $user->sid;
$method = 'ap_user.get';
$hash = hash_hmac('sha256', $timestamp.';'.$domain.';'.$nonce.';'.$method, $api_key);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $webservice);
$data = array(
'method' => '"'.$method.'"',
'hash' => '"'.$hash.'"',
'domain_name' => '"'.$domain.'"',
'domain_time_stamp' => '"'.$timestamp.'"',
'nonce' => '"'.$nonce.'"',
// 'sessid' => '"'.$sid.'"',
'mail' => '"foo@bar.com"'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
var_dump($result);
?>
- I have already unchecked the Session checkbox on Keys Settings
- I have granted access to the Anonymous user for user_services module.
However is still returning the following error:
string(77) "{ "#error": false, "#data": { "#error": true, "#message": "Access denied" } }"Note: dis-commenting the:
// 'sessid' => '"'.$sid.'"',will make the code run perfectly! =(Any ideas?
Thank you
Wils
Never is to late for refactoring =D
Fixed
Hello there.
I found the problem and fixed It.
I am posting here because I believe can be helpful to somebody in the future =D
I have a custom service and it was not implementing all hook_service correctly.
I needed to add the following params:
'#callback' => 'ap_user_service_get','#access callback' => 'ap_user_service_get_access',
This fixed my problem.
I was passing wrong function on callback and access callback.
Also I have written this article to help somebody in the future:
http://www.wilsolutions.com.br/index.php?q=node/9
tkx
Wils
Wils
Never is to late for refactoring =D
Return fields as array
I'm trying to return only some fields but it isn't working, I get the whole node instead of only the fields I want. The important part of the code is:
<?php
// ...
$data = array(
'method' => '"node.get"',
'nid' => '"'.$nid.'"',
'fields' => array(
'nid', 'status',
),
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// ...
?>
Can someone please explain how to build the fields array please?
Luis
lelizondo, I haven't been
lelizondo, I haven't been able to get it working either. I think the issue is that curl behaves unusually when receiving an array instead of a string or integer as a $value in one of the $key => $value pairs in $data. Here's a discussion of the issue:
http://matthewturland.com/2010/06/30/gotcha-on-scraping-net-applications...
----------------
IT consultant, web designer, writer and researcher
http://www.sheldonrampton.com/portfolio
json_encode arrays
You'll need to JSON encode the fields array:
json_encode(array('nid', 'status'))
before passing it to the service
Does anyone know the correct
Does anyone know the correct format for the parameters? No matter which tutorial/example I follow, json-rpc tells me either my json format is invalid or it didn't receive any parameters. This is for node.get. Thanks