예제 #1
0
 /**
  * Send an http response with content encoded as JSON
  *
  * @param mixed $content           
  */
 public static function sendResponse($content)
 {
     $app = \Slim\Slim::getInstance();
     $app->response()->header('Content-Type', 'application/json');
     $jsonResponse = json_encode($content, JSON_NUMERIC_CHECK);
     // AppUtils::logDebug($jsonResponse);
     $jsonErr = json_last_error();
     if ($jsonErr != JSON_ERROR_NONE) {
         $errStr = '';
         switch ($jsonErr) {
             case JSON_ERROR_DEPTH:
                 $errStr = ' - Maximum stack depth exceeded';
                 break;
             case JSON_ERROR_STATE_MISMATCH:
                 $errStr = ' - Underflow or the modes mismatch';
                 break;
             case JSON_ERROR_CTRL_CHAR:
                 $errStr = ' - Unexpected control character found';
                 break;
             case JSON_ERROR_SYNTAX:
                 $errStr = ' - Syntax error, malformed JSON';
                 break;
             case JSON_ERROR_UTF8:
                 $errStr = ' - Malformed UTF-8 characters, possibly incorrectly encoded';
                 break;
             default:
                 $errStr = ' - Unknown error';
                 break;
         }
         AppUtils::logDebug("JSON ERROR " . $errStr);
     }
     // Include support for JSONP requests
     if (!isset($_GET['callback'])) {
         echo $jsonResponse;
     } else {
         echo $_GET['callback'] . '(' . $jsonResponse . ');';
     }
 }