'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($api_response); // Deliver formatted data // echo $json_response; // echo $api_response; echo CJSON::encode ( $api_response ); } 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 = '' . "\n" . '' . "\n" . "\t" . '' . $api_response ['code'] . '' . "\n" . "\t" . '' . $api_response ['data'] . '' . "\n" . ''; // 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 utf8_decode ( $api_response ['data'] ); } // End script process exit (); } /* * // Set default HTTP response of 'ok' * $response['code'] = 0; * $response['status'] = 404; * $response['data'] = NULL; */ // --- Step 2: Authorization /* * // Optionally require connections to be made via HTTPS * if( $HTTPS_required && $_SERVER['HTTPS'] != 'on' ){ * $response['code'] = 2; * $response['status'] = $api_response_code[ $response['code'] ]['HTTP Response']; * $response['data'] = $api_response_code[ $response['code'] ]['Message']; * * // Return Response to browser. This will exit the script. * deliver_response($_GET['format'], $response); * } * * // Optionally require user authentication * if( $authentication_required ){ * * if( empty($_POST['username']) || empty($_POST['password']) ){ * $response['code'] = 3; * $response['status'] = $api_response_code[ $response['code'] ]['HTTP Response']; * $response['data'] = $api_response_code[ $response['code'] ]['Message']; * * // Return Response to browser * deliver_response($_GET['format'], $response); * * } * * // Return an error response if user fails authentication. This is a very simplistic example * // that should be modified for security in a production environment * elseif( $_POST['username'] != 'foo' && $_POST['password'] != 'bar' ){ * $response['code'] = 4; * $response['status'] = $api_response_code[ $response['code'] ]['HTTP Response']; * $response['data'] = $api_response_code[ $response['code'] ]['Message']; * * // Return Response to browser * deliver_response($_GET['format'], $response); * * } * * } */ // --- Step 3: Process Request // --- Step 4: Deliver Response // Return Response to browser // deliver_response($_GET['format'], $response); } ?>