Monday, 15 September 2014

Creation of a REST/JSON webservice

Here below, I will show how to create a webservice with data in JSON format through a simple example (a function which gives the double of a given number):


 code for creation_service_rest.php

<?php


function double($val)

{

   return $val * 2;

}

if($_GET['arg']!='' && $_GET['function']=='double')

{

   $value = @call_user_func($_GET['function'], $_GET['arg']);

   $result['response']['value']=$value;
   $result['error']['code'] = '0';
   $result['error']['message'] = '0K';

}

else

{

   $result['response']['value']=NULL;
   $result['error']['code'] = '1';
   $result['error']['message'] = "RequĂȘte Invalide". ' avec function '.$_GET['function'].' et argument '.$_GET['arg'];

}

 echo json_encode($result);
?>

cf. http://notos.fr/blog/index.php?article23/exemple-de-web-services-en-php