protected function response($response) { if ($format = waRequest::get('format')) { $format = strtoupper($format); if (!in_array($format, array('JSON', 'XML'))) { $response = array('error' => 'invalid_request', 'error_description' => 'Invalid format: ' . $format); $format = 'JSON'; } } echo waAPIDecorator::factory($format)->decorate($response); }
public function __toString() { if ($this->status_code) { wa()->getResponse()->setStatus($this->status_code); } $format = strtoupper(waRequest::request('format', 'JSON')); if ($format && !in_array($format, array("XML", "JSON"))) { $this->error = 'invalid_request'; $this->error_description = 'Invalid response format: ' . $format; $format = 'JSON'; } if (!$format) { $format = 'JSON'; } $result = ''; if ($format == 'XML') { wa()->getResponse()->addHeader('Content-type', 'text/xml; charset=utf-8'); } elseif ($format == 'JSON') { $callback = (string) waRequest::get('callback', false); // for JSONP if ($callback) { wa()->getResponse()->setStatus(200); wa()->getResponse()->addHeader('Content-type', 'text/javascript; charset=utf-8'); $result .= $callback . '('; } else { wa()->getResponse()->addHeader('Content-type', 'application/json; charset=utf-8'); } } $response = array('error' => $this->error); if ($this->error_description) { $response['error_description'] = $this->error_description; } wa()->getResponse()->sendHeaders(); $result .= waAPIDecorator::factory($format)->decorate($response); if (!empty($callback)) { $result .= ');'; } return $result; }
protected function response($response, $code = null) { if ($code) { wa()->getResponse()->setStatus($code); } if ($this->format == 'XML') { wa()->getResponse()->addHeader('Content-type', 'text/xml; charset=utf-8'); } elseif ($this->format == 'JSON') { $callback = (string) waRequest::get('callback', false); // for JSONP if ($callback) { wa()->getResponse()->setStatus(200); wa()->getResponse()->addHeader('Content-type', 'text/javascript; charset=utf-8'); echo $callback . '('; } else { wa()->getResponse()->addHeader('Content-type', 'application/json; charset=utf-8'); } } wa()->getResponse()->sendHeaders(); echo waAPIDecorator::factory($this->format)->decorate($response); if (!empty($callback)) { echo ');'; } }