Ejemplo n.º 1
0
 /**
  * Sends a HTTP response back to the client.
  *
  * @param integer $status The status code to use
  * @param type $message
  * @param type $error
  * @throws Exception
  */
 public function sendHttp($status = null, $message = '', $error = null)
 {
     self::$_responding = true;
     // Close the output buffer; it's now safe to do so, since the header
     // will soon be sent.
     $output = ob_get_clean();
     ob_end_clean();
     $extraOutput = self::$includeExtraneousOutput && !empty($output);
     $status = $status === null ? (bool) $error ? self::$errorCode : 200 : $status;
     // Set the response content
     if ($status !== null && !array_key_exists((int) $status, self::$_statusMessages)) {
         // Invalid call to this method. Fail noisily.
         $this->_status = self::$errorCode;
         $body = '{"error":true,"message":"Internal server error: invalid or ' . 'non-numeric HTTP response status code specifed.","status":500}';
     } else {
         if (!extension_loaded('json') || isset($this->body)) {
             // We might be doing something other than responding in JSON
             if (!isset($this->body)) {
                 if (strpos($this->httpHeader['Content-Type'], 'application/json') === 0) {
                     // JSON-format responding in use but not available
                     $this->_status = self::$errorCode;
                     $body = '{"error":true,"message":"The JSON PHP extension is required,' . ' but this server lacks it.","status":' . $this->_status . '}';
                 } else {
                     // Simply echo the message if JSON isn't available.
                     $this->_status = $status;
                     $body = ($extraOutput ? $output . ' ' : '') . $message;
                 }
             } else {
                 // The "body" property is in use, which overrides the standard
                 // way of responding with JSON-encoded properties
                 $this->_status = $status;
                 $body = ($extraOutput ? $output . ' ' : '') . $this->body;
             }
         } else {
             if ($status != null) {
                 // Override status. Loose comparison is in use because zero is
                 // an invalid HTTP response code and expected only of certain
                 // cURL libraries when the connection could not be established.
                 $this->_status = $status;
             }
             $response = $this->_properties;
             // Set universal response properties:
             if (empty($message) && !empty($response['message'])) {
                 $message = $response['message'];
             }
             $response['message'] = $message . ($extraOutput ? " Note, extraneous output was generated in the scope of this response: {$output}" : '');
             $response['error'] = $error === null ? $this->_status >= 400 : (bool) $error;
             // Include the status code in the envelope for clients that can't
             // read HTTP headers:
             $response['status'] = $this->_status;
             // Compose the body of the response as a JSON-encoded object:
             $body = json_encode($response);
         }
     }
     // Send the response
     $this->sendHttpHeader();
     echo $body;
     // Shut down
     self::$_response = null;
     self::end();
 }