Exemple d'utilisation de l'API REST en Php

Attention se script n'est qu'un exemple non exhaustif d'utilisation de l'API REST en php, il n'est en aucun cas à utiliser tel quel en Production.

<?php
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License (LGPL) as
//    published by the Free Software Foundation, either version 2 of the License,
//    or (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//    See <http://www.gnu.org/licenses/lgpl.html/>.
//    (c) Accelya 2010
 
/**
* Appelle une méthode de l'API rest du Hub Numérique Dilicom.
*
* $method : Nom de la méthode (getOrderDetail, sendOrder, ... ) 
* $params : Tableau contenant les paramètres.
*
* retour :  Un objet PHP correspondant à l'Objet JSON renvoyé par l'appel au Service Web.
*/
function callMethod($method,$params) {
  $query_string = "https://hub-dilicom.centprod.com/v1/hub-numerique-api/json/".$method."?";
  $login = "gln"; //Indiquer votre GLN
  $password = "password"; //indiquer votre mot de passe
  foreach ($params as $key => $value) {
	  $query_string .= "$key=" . urlencode($value) . "&";
  }
  $headers = array(
  'Accept: application/json'
  );
 
 
  $ch = curl_init();     
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  curl_setopt($ch, CURLOPT_URL, $query_string);  
  curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");  
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  $result = curl_exec($ch);  
  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);  
 
  if($result === FALSE) {
	die("Une erreur est survenue");
  } else if (substr($code,0,1) != "2") {
	die("Return HTTP code ".$code);
  } else {
	return json_decode($result);
  } 
}
 
?>
<h3>Get notices</h3>
<pre>
<? print_r( callMethod("getNotices",array("initialization" => ""))); ?>
<pre>
<h3> Send Order </h3>
<pre>
<?
print_r(  callMethod("sendOrder",array(
  "glnGroup" => "6235482301523",
  "orderId" => "ORD00256",
  "customerId" => "test",
  "finalBookOwner.identifier" => "test",
  "finalBookOwner.civility" => "M",
  "finalBookOwner.firstName" => "John",
  "finalBookOwner.lastName" => "Doe",
  "finalBookOwner.email" => "john@doe.com",
  "finalBookOwner.country" => "France",
  "finalBookOwner.city" => "Paris",
  "finalBookOwner.postalCode" => "75001",
  "orderRequestLines[0].ean13" => "9782814500136",
  "orderRequestLines[0].glnDistributor" => "3015594118311",
  "orderRequestLines[0].quantity" => "1",
  "orderRequestLines[0].unitPrice" => "1",
  "orderRequestLines[0].lineReference" => "01",
  "orderRequestLines[1].ean13" => "9990000000001",
  "orderRequestLines[1].glnDistributor" => "3012410002007",
  "orderRequestLines[1].quantity" => "1",
  "orderRequestLines[1].lineReference" => "02",
  "orderRequestLines[1].unitPrice" => "1")));
?>
</pre>