SMEStorage php sample PDF Print E-mail

I have did some libs for SMEStorage.com API . Also i communicated with programmers that used SMEStorage API and i found that most difficult thing is to start using this API. For example, creating a folder or uploading a file. And next things are easy is this first tasks are done.

There i will describe how to create folder in SMEStorage with php.

I will create simple php library for this. Full SMEStorage API description should be there http://code.google.com/p/smestorage/

Main file (test.php) that  will test library has code:

<?

include('lib.php');

//get token for user
$login='gelembjuk2';
$password='25121982';

if(!getToken($login,$password)){
 echo 'Can not get token. Maybe login/password incorrect';
 exit;
}

//create folder in root
$folderid=createFolder('my test folder','0');

//create subfolder in just created folder
$folderid=createFolder('my test subfolder',$folderid);

?>

Lets analize library lib.php line by line.

define('_SERVICE_URL','http://www.smestorage.com/api/');

require('class.xmltoarray.php');
global $token;

First line defines API base url.

Then including php class that parses xml. It will be used to parse API response.

And last line there is declaration of the variable where API token will be stored. One get, the token is used for all API calling during a session.
Next part of the code:


function processRequest($request,$data=array()){
 $result=array('',array());

 $url=_SERVICE_URL.$request;

 $process = curl_init($url); 
 
 curl_setopt($process, CURLOPT_USERAGENT, 'SMEStorage client example'); 
 
 if(count($data)>0){
 //this is post request
 curl_setopt($process, CURLOPT_POST,     1 );
 curl_setopt($process, CURLOPT_POSTFIELDS, $data); 
 }
 curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
 curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
 
 $response = curl_exec($process); 
 if(curl_error($process)){
 echo curl_error($process);
 }
 curl_close($process); 
 
 //parse response
 $x=new XmlToArray($response);
 $a=$x->createArray();

 $a=$a['response'];
 if($a['status']!='ok')
 $result[0]=$a['statusmessage'];
 
 $result[1]=$a;

 return $result;    
}

This is function that sends API request and parse response.

The function can be splited for 2 parts: first part sends request and gets response and second part parses xml response.

Function gets API request url and only adds correct API server host to the url. Combining of the url is in response of functions that will call function processRequest .

SMEStorage API url is in format:

http://www.smestorage.com/api/TOKEN/FUNCTIONNAME/ARGUMENTS

Next is function that does user Auth.

function getToken($login,$password){
 global $token;
 $a= processRequest('*/gettoken/'.encodeArgs(array($login,$password)));

 $token='';
 if($a[0]==''){
 $token=$a[1]['token'];
 return true;
 }
 return false;
}

This is function that requests a token for user session. User login and password are sent as arguments.

API request url for getting token is:

http://www.smestorage.com/api/*/gettoken/BASE64ENCODEDLOGIN,BASE64ENCODEDPASSWORD

There it is needed to describe function encodeArgs that encoded arguments set to required format. Arguments are set without names, so argument order is important. Read more in API manual.

function encodeArgs($args){
 $a=array();
 foreach ($args as $ar){
 $a[]=base64_encode($ar);
 }
 return join(',',$a);
}

At last there is function that creates a folder.

API function requires 3 arguments: folder name, folder description and parent folder id.


function createFolder($foldername,$parentid='0'){
 global $token;
 $a= processRequest($token.'/doCreateNewFolder/'.encodeArgs(array(
 $foldername,        //1-st arg is folder name
 '',            //2-nd arg is folder descrition
 $parentid        //3-rd arg is folder parent folder id
 )));

 if($a[0]==''){
 //return new folder id
 return $a[1]['file']['fi_id'];
 }
 return false;
}

Full example code can be downloaded there in Downloads area - download SMEStorage API sample 1

 

Last Updated on Friday, 12 February 2010 12:46
 

Comments  

 
#1 federico 2012-03-13 17:42
Utili servizi di consulenza scolastica e recupero anni scolastici per la scuola superiore. [http://www.scuolaonline.com/diploma-online/diploma-online]Diploma Online[/http://www.scuolaonline.com/diploma-online/diploma-online] anche in un solo anno.
Laurea Online in due anni.