Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.04 KB | None | 0 0
  1. <?php
  2.     $ok = false;
  3.     $user = 'root';
  4.     $pass = '';
  5.     $dbh = new PDO('mysql:host=localhost;dbname=partielws', $user, $pass);
  6.    
  7.     // Fonction erreur
  8.      function invalidParams(){
  9.        $params = array("status"=>-1,"msg"=>"Invalid Parameters Supplied");
  10.        print json_encode($params);
  11.        exit;
  12.     }
  13.     //Récup paramètres méthode avec GET / ?
  14.     if(isset($_GET['cp']) && $_GET['cp']!='' && !$ok)
  15.     {
  16.         $cp = $_GET['cp'];
  17.         $req = $dbh->query('SELECT * FROM commune WHERE cp=' . $cp);  
  18.         $ok = true;
  19.         $critere = "cp";
  20.     }
  21.     if(isset($_GET['ville']) && $_GET['ville']!='' && !$ok)
  22.     {
  23.         $ville = $_GET['ville'];
  24.         $req = $dbh->query('SELECT * FROM commune WHERE ville="' . $ville .'"');
  25.         $ok = true;
  26.         $critere = "ville";
  27.     }
  28.     if(isset($_GET['nbHab']) && $_GET['nbHab']!='' && !$ok)
  29.     {
  30.         $nbHab = $_GET['nbHab'];
  31.         $req = $dbh->query('SELECT * FROM commune WHERE nbHab=' . $nbHab);
  32.         $ok = true;
  33.         $critere = "nbHab";
  34.     }
  35.     if(isset($_GET['superficie']) && $_GET['superficie']!='' && !$ok)
  36.     {
  37.         $superficie = $_GET['superficie'];
  38.         $req = $dbh->query('SELECT * FROM commune WHERE cp=' . $superficie);
  39.         $ok = true;
  40.         $critere = "superficie";
  41.     }
  42.     if(isset($_GET['format']) && $_GET['format']!='' && !$ok)
  43.     {
  44.         $format = $_GET['format'];
  45.     }
  46.    
  47. function deliver_response($format, $api_response){
  48.  
  49.     // Define HTTP responses
  50.     $http_response_code = array(
  51.         200 => 'OK',
  52.         400 => 'Bad Request',
  53.         401 => 'Unauthorized',
  54.         403 => 'Forbidden',
  55.         404 => 'Not Found'
  56.     );
  57.  
  58.     // Set HTTP Response
  59.     header('HTTP/1.1 '.$api_response['status'].' '.$http_response_code[ $api_response['status'] ]);
  60.  
  61.     // Process different content types
  62.     if( strcasecmp($format,'json') == 0 ){
  63.  
  64.         // Set HTTP Response Content Type
  65.         header('Content-Type: application/json; charset=utf-8');
  66.  
  67.         // Format data into a JSON response
  68.         $json_response = json_encode($req->fetch());
  69.  
  70.     }elseif( strcasecmp($format,'xml') == 0 ){
  71.          // Set HTTP Response Content Type
  72.         header('Content-Type: application/xml; charset=utf-8');
  73.  
  74.         // Format data into an XML response (This is only good at handling string data, not arrays)
  75.         $xml_response = '<?xml version="1.0" encoding="UTF-8"?>'."\n".
  76.             '<response>'."\n".
  77.             "\t".'<code>'.$api_response['code'].'</code>'."\n".
  78.             "\t".'<data>'.$api_response['data'].'</data>'."\n".
  79.             '</response>';
  80.  
  81.         // Deliver formatted data
  82.         //echo $xml_response;
  83.      } else {
  84.          // Set HTTP Response Content Type (This is only good at handling string data, not arrays)
  85.         header('Content-Type: text/html; charset=utf-8');
  86.  
  87.         // Deliver formatted data
  88.         //echo $api_response['data'];
  89.     }
  90.      // End script process
  91.     exit;
  92.  }
  93.  
  94. // Define API response codes and their related HTTP response
  95. $api_response_code = array(
  96.     0 => array('HTTP Response' => 400, 'Message' => 'Unknown Error'),
  97.     1 => array('HTTP Response' => 200, 'Message' => 'Success'),
  98.     2 => array('HTTP Response' => 403, 'Message' => 'HTTPS Required'),
  99.     3 => array('HTTP Response' => 401, 'Message' => 'Authentication Required'),
  100.     4 => array('HTTP Response' => 401, 'Message' => 'Authentication Failed'),
  101.     5 => array('HTTP Response' => 404, 'Message' => 'Invalid Request'),
  102.     6 => array('HTTP Response' => 400, 'Message' => 'Invalid Response Format')
  103. );
  104.  
  105. // Set default HTTP response
  106. $response['code'] = 0;
  107. $response['status'] = 404;
  108. $response['data'] = NULL;
  109.  
  110.  // --- Step 3: Process Request - Method A: Say Hello to the API
  111. if( $ok ){
  112.     $response['code'] = 1;
  113.     $response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
  114.     $response['data'] = $req->fetch();
  115. }
  116.  
  117. // --- Step 4: Deliver Response - Return Response to browser
  118. deliver_response($_GET['format'], $response);
  119.  
  120. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement