$ji_db->query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'"); $ji_db->query('SET CHARACTER SET utf8'); // collect URL and headers $request = new Request(); // set some default parameters $request->parameters['resultsperpage'] = $request->getParameter('resultsperpage', 20); $request->parameters['start'] = $request->getParameter('start', 0); // identify our user if applicable $headers = apache_request_headers(); if (isset($headers['Authorization'])) { $request->identifyUser($ji_db, $headers['Authorization']); } // Which content type to return? Parameter takes precedence over accept headers // with final fall back to json $format_choices = array('application/json', 'text/html'); $header_format = $request->preferredContentTypeOutOf($format_choices); $format = $request->getParameter('format', $header_format); switch ($format) { case 'text/html': case 'html': $request->view = new HtmlView(); break; case 'application/json': case 'json': default: // JSONP? $callback = filter_var($request->getParameter('callback'), FILTER_SANITIZE_STRING); if ($callback) { $request->view = new JsonPView($callback); } else { $request->view = new JsonView();
/** * Ensures that if the browser doesn't send an accept header we can deal with * we return json * * @return void * * @test * @backupGlobals */ public function ifPreferredFormatIsNotAcceptedReturnJson() { $server = ['HTTP_ACCEPT' => 'text/text,application/xhtml+xml,application/json;q=0.9,*/*;q=0.8']; $request = new \Request($this->config, $server); $result = $request->preferredContentTypeOutOf(array('text/html'), array('application/xml')); $this->assertEquals('json', $result); }