Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $ok = false;
- $user = 'root';
- $pass = '';
- $dbh = new PDO('mysql:host=localhost;dbname=partielws', $user, $pass);
- // Fonction erreur
- function invalidParams(){
- $params = array("status"=>-1,"msg"=>"Invalid Parameters Supplied");
- print json_encode($params);
- exit;
- }
- //Récup paramètres méthode avec GET / ?
- if(isset($_GET['cp']) && $_GET['cp']!='' && !$ok)
- {
- $cp = $_GET['cp'];
- $req = $dbh->query('SELECT * FROM commune WHERE cp=' . $cp);
- $ok = true;
- $critere = "cp";
- }
- if(isset($_GET['ville']) && $_GET['ville']!='' && !$ok)
- {
- $ville = $_GET['ville'];
- $req = $dbh->query('SELECT * FROM commune WHERE ville="' . $ville .'"');
- $ok = true;
- $critere = "ville";
- }
- if(isset($_GET['nbHab']) && $_GET['nbHab']!='' && !$ok)
- {
- $nbHab = $_GET['nbHab'];
- $req = $dbh->query('SELECT * FROM commune WHERE nbHab=' . $nbHab);
- $ok = true;
- $critere = "nbHab";
- }
- if(isset($_GET['superficie']) && $_GET['superficie']!='' && !$ok)
- {
- $superficie = $_GET['superficie'];
- $req = $dbh->query('SELECT * FROM commune WHERE cp=' . $superficie);
- $ok = true;
- $critere = "superficie";
- }
- if(isset($_GET['format']) && $_GET['format']!='' && !$ok)
- {
- $format = $_GET['format'];
- }
- function deliver_response($format, $api_response){
- // Define HTTP responses
- $http_response_code = array(
- 200 => 'OK',
- 400 => 'Bad Request',
- 401 => 'Unauthorized',
- 403 => 'Forbidden',
- 404 => 'Not Found'
- );
- // Set HTTP Response
- header('HTTP/1.1 '.$api_response['status'].' '.$http_response_code[ $api_response['status'] ]);
- // Process different content types
- if( strcasecmp($format,'json') == 0 ){
- // Set HTTP Response Content Type
- header('Content-Type: application/json; charset=utf-8');
- // Format data into a JSON response
- $json_response = json_encode($req->fetch());
- }elseif( strcasecmp($format,'xml') == 0 ){
- // Set HTTP Response Content Type
- header('Content-Type: application/xml; charset=utf-8');
- // Format data into an XML response (This is only good at handling string data, not arrays)
- $xml_response = '<?xml version="1.0" encoding="UTF-8"?>'."\n".
- '<response>'."\n".
- "\t".'<code>'.$api_response['code'].'</code>'."\n".
- "\t".'<data>'.$api_response['data'].'</data>'."\n".
- '</response>';
- // Deliver formatted data
- //echo $xml_response;
- } else {
- // Set HTTP Response Content Type (This is only good at handling string data, not arrays)
- header('Content-Type: text/html; charset=utf-8');
- // Deliver formatted data
- //echo $api_response['data'];
- }
- // End script process
- exit;
- }
- // Define API response codes and their related HTTP response
- $api_response_code = array(
- 0 => array('HTTP Response' => 400, 'Message' => 'Unknown Error'),
- 1 => array('HTTP Response' => 200, 'Message' => 'Success'),
- 2 => array('HTTP Response' => 403, 'Message' => 'HTTPS Required'),
- 3 => array('HTTP Response' => 401, 'Message' => 'Authentication Required'),
- 4 => array('HTTP Response' => 401, 'Message' => 'Authentication Failed'),
- 5 => array('HTTP Response' => 404, 'Message' => 'Invalid Request'),
- 6 => array('HTTP Response' => 400, 'Message' => 'Invalid Response Format')
- );
- // Set default HTTP response
- $response['code'] = 0;
- $response['status'] = 404;
- $response['data'] = NULL;
- // --- Step 3: Process Request - Method A: Say Hello to the API
- if( $ok ){
- $response['code'] = 1;
- $response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
- $response['data'] = $req->fetch();
- }
- // --- Step 4: Deliver Response - Return Response to browser
- deliver_response($_GET['format'], $response);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement