PHP - how-to

Below you will find some Php example code, to call the API in PHP.

Configure PHP

In order to use the code below, you need to activate the CURL function in PHP, to do so open your php.ini

                           ;PHPExt 
                            extension=php_bz2.dll
                            extension=php_curl.dll        // You need to make sure this line is not commented
                            ;extension=php_fileinfo.dll
                            extension=php_gd2.dll
            

Source code

Get data from the api (Right click & save the full source code here)


	//Step 1 : Authorization
$basicAuth = $api_key . ';' . $username . ':' . $password;
$basicAuth = 'Basic '.base64_encode($basicAuth);

//Step2 : Make your request
$req = 'json_header={action:"show"}&json_data={FidelityMember:{name:"DUPOND"}}'; //Here we are looking for every persons with the name "DUPOND"

//Step 3 : Sending data
$curl = curl_init($server_url); //Url of Adelya API
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: $basicAuth")); //We place the Authorization into HTTP HEADER
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $req);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);   // This allow to bypass server certificates
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); //Getting Status of Request
	
            

Save data though the api (Right click & save full source code here)


	//Step 1 : Authorization
$basicAuth = $api_key . ';' . $username . ':' . $password;
$basicAuth = 'Basic '.base64_encode($basicAuth);

//Step2 : Make your request
//Here we are going to set the name of client n°3284180 to "DUPOND"
$req = 'json_header={action:"persist"}&json_data={FidelityMember:{id: "3284180",name:"DUPOND"}}';

//Important : If no id is specified, the api will create instead a new customer named "DUPOND"

//Step 3 : Sending data
$curl = curl_init($server_url); //Url of Adelya API
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: $basicAuth")); //We place the Authorization into HTTP HEADER
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $req);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);   // This allow to bypass server certificates
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); //Getting Status of Request
	
            

Parse API results


$response = curl_exec($curl);
$api_answer = json_decode($response, true);
if (isset($api_answer) && isset($api_answer['ApiReturn']) && isset($api_answer['ApiReturn']['code']) && $api_answer['ApiReturn']['code'] == 'OK') {
	//Everything is Ok
	echo 'Hello my name is '.$api_answer['FidelityMember']['name']; //Display : Hello my name is DUPOND
} else if(isset($api_answer) && isset($api_answer['ApiReturn']) && isset($api_answer['ApiReturn']['code'])
&& $api_answer['ApiReturn']['code'] == 'ERROR') {
	echo 'The API call cannot be fulfilled : '.$api_answer['ApiReturn']['message'];
} else {
	//Something very bad has happened
	die("API ERROR : $response");
}
	
            

Post files to url


	//Step 1 : Authorization
$basicAuth = $api_key . ';' . $username . ':' . $password;
$basicAuth = 'Basic '.base64_encode($basicAuth);

//Step2 : Prepare your file
//Here we are going to post a file named adelya.zip  (of course you need to set the path to your file)
$post = array(
    'userfile' => '@/myfolder/adelya.zip'      // userfile represents the upload input name on a standard form.   Keep @ in the path
);


//Step 3 : Sending data
$curl = curl_init($server_url); //Url of Adelya API
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: $basicAuth")); //We place the Authorization into HTTP HEADER
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);  //Posting file here
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);   // This allow to bypass server certificates
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); //Getting Status of Request