This PHP API Development Kit requires PHP 5.2 or higher and CURL extension. This toolkit provides some basic functions to perform API calls and user authentication. Results can be returned in JSON, XML or serialized PHP.
Keep your API secret safe.
pear install -of https://s.ipernity.com/E/API_Kits/PHP/Iper_API/Iper_API-latest.tgz
Download Iper_API, unzip (tar/gz) and copy API.php
in your website directory.
require 'API.php'; $api = new Iper_API(array('api_key'=>'8da10d4c00000001155dac4aa33cb785', 'secret'=>'966f6ff8c3ceba3a', 'format'=>'json')); $r = $api->request('test.echo',array('echo'=>"you're smart!")); if ( $r ) { print 'API replied: '.$r['echo']; } else { print 'An error occured: '.$api->getErrorMessage(); }
The class constructor. Uses
setConf(conf)
to set configuration values.
conf
is anarray()
of configuration parameters:JSON responses are decoded using
api_key
: your API key.secret
: your secret.format
: the desired output format. Values are :xml
,json
orphp
. Default isjson
.json_decode(data,true)
withassoc
boolean true by default. Change the class property$api->_json_assoc
tofalse
if you prefer to receive a JSON object.
Enter your application name if you'd like it to be sent in the HTTP User-Agent header.
Parameters :
The request class method will take care of signing your request.
method
: the API method name.params
: an array of parameters to send. (no need to preciseapi_url
).with_auth_token
: if set totrue
, automatically add theauth_token
parameter to the request.Returns :
false
: if the request failed. CheckgetErrorCode()
andgetErrorMessage()
to see what happened.object|array
: the result is sent in the requested format (xml/json/php).
Parameters :
perms
: an array of permissions. ex :['perm_doc']=>'write', 'perm_network'=>'read']
.frob
: an optional frob (for desktop authentication).Returns : an URL
Uses the
request()
method to perform anauth.getFrob
API call. Returns afrob
orfalse
.
Uses the
request()
method to perform anauth.getToken
API call. Returns atoken
orfalse
. When successful, the authentication information is stored into the$this->_auth
property.
Authenticated calls can be done by adding theauth_token
in the request parameters or by setting the flagwith_auth_token
(see therequest()
function).
If the
token
is omitted the function will check the current token (ifgetToken()
was called earlier). Returns atrue
orfalse
. When successful, the authentication information is stored into the$this->_auth
property.
Returns the
$this->_auth
array.
Removes authentication information e.g. resets
$this->_auth
.
// we assume this script is your callback url. require 'API.php'; $api=new Iper_API(array('api_key'=>'6fa87ba500002712bd4eed6020f3bd72', 'secret'=>'e9a599f0cf6ce193')); $api->setAppName('My supercool app'); // authenticate if ( $_GET['frob'] ) { $token = $api->getToken($_GET['frob']); if ( ! $token ) die($api->getErrorMessage()); // call with auto-authentication $r = $api->request('account.getQuota',array(),true); var_dump($r); } else { $url = $api->getAuthUrl(array('doc'=>'write')); print '<a href="'.$url.'">click here to authorize this app</a>'; }
Have fun and let us know if you need any help.