public function sendError(ResponseItem $responseItem) { $unauthorized = false; $errorMessage = $responseItem->getErrorMessage(); $errorCode = $responseItem->getError(); switch ($errorCode) { case ResponseError::$BAD_REQUEST: $code = '400 Bad Request'; break; case ResponseError::$UNAUTHORIZED: $code = '401 Unauthorized'; $unauthorized = true; break; case ResponseError::$FORBIDDEN: $code = '403 Forbidden'; break; case ResponseError::$NOT_FOUND: $code = '404 Not Found'; break; case ResponseError::$NOT_IMPLEMENTED: $code = '501 Not Implemented'; break; case ResponseError::$INTERNAL_ERROR: default: $code = '500 Internal Server Error'; break; } @header("HTTP/1.0 {$code}", true); if ($unauthorized) { header("WWW-Authenticate: OAuth realm", true); } echo "{$code} - {$errorMessage}"; die; }
function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { $doc = $this->createXmlDoc(); $requestType = $this->getRequestType($requestItem); $data = $responseItem->getResponse(); // Check to see if this is a single entry, or a collection, and construct either an xml // feed (collection) or an entry (single) if ($responseItem->getResponse() instanceof RestfulCollection) { $totalResults = $responseItem->getResponse()->getTotalResults(); $itemsPerPage = $requestItem->getCount(); $startIndex = $requestItem->getStartIndex(); // The root Feed element $entry = $this->addNode($doc, 'response', ''); // Required Xml fields $this->addNode($entry, 'startIndex', $startIndex); $this->addNode($entry, 'itemsPerPage', $itemsPerPage); $this->addNode($entry, 'totalResults', $totalResults); $responses = $responseItem->getResponse()->getEntry(); foreach ($responses as $response) { // recursively add responseItem data to the xml structure $this->addData($entry, $requestType, $response); } } else { // Single entry = Xml:Entry $entry = $this->addNode($doc, 'response', ''); // addData loops through the responseItem data recursively creating a matching XML structure $this->addData($entry, 'entry', $data['entry']); } $xml = $doc->saveXML(); echo $xml; }
/** * * @param ResponseItem $responseItem * @param RestRequestItem $requestItem */ function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { $response = $responseItem->getResponse(); if ($response instanceof RestfulCollection) { $itemsPerPage = $requestItem->getCount(); if ($itemsPerPage > 0) { $response->itemsPerPage = $itemsPerPage; } } // several service calls return a null value if (!is_null($response)) { $this->encodeAndSendResponse($response); } }
static function all($response_ID) { $return = array(); foreach (ReadWrite::all("ResponseItems", array("ResponseId"), array($response_ID)) as $ID) { $return[] = ResponseItem::load($ID); } return $return; }
function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { $response = $responseItem->getResponse(); if ($response instanceof RestfulCollection) { $itemsPerPage = $requestItem->getCount(); if ($itemsPerPage > 0) { $response->itemsPerPage = $itemsPerPage; } } // several service calls return a null value if (!is_null($response)) { if (Config::get('debug')) { echo self::json_format(json_encode($response)); // TODO: add a query option to pretty-print json output } else { echo json_encode($response); } } }
function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { $doc = $this->createAtomDoc(); $requestType = $this->getRequestType($requestItem); $data = $responseItem->getResponse(); $params = $requestItem->getParameters(); $userId = isset($params['userId']) ? $params['userId'][0] : ''; $guid = 'urn:guid:' . $userId; $authorName = $_SERVER['HTTP_HOST'] . ':' . $userId; $updatedAtom = date(DATE_ATOM); // Check to see if this is a single entry, or a collection, and construct either an atom // feed (collection) or an entry (single) if ($data instanceof RestfulCollection) { $totalResults = $data->getTotalResults(); $itemsPerPage = $requestItem->getCount(); $startIndex = $requestItem->getStartIndex(); // The root Feed element $entry = $this->addNode($doc, 'feed', '', false, self::$nameSpace); // Required Atom fields $endPos = $startIndex + $itemsPerPage > $totalResults ? $totalResults : $startIndex + $itemsPerPage; $this->addNode($entry, 'title', $requestType . ' feed for id ' . $authorName . ' (' . $startIndex . ' - ' . ($endPos - 1) . ' of ' . $totalResults . ')'); $author = $this->addNode($entry, 'author'); $this->addNode($author, 'uri', $guid); $this->addNode($author, 'name', $authorName); $this->addNode($entry, 'updated', $updatedAtom); $this->addNode($entry, 'id', $guid); $this->addNode($entry, 'link', '', array('rel' => 'self', 'href' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])); // Add osearch & next link to the entry $this->addPagingFields($entry, $startIndex, $itemsPerPage, $totalResults); // Add response entries to feed $responses = $data->getEntry(); foreach ($responses as $response) { // Attempt to have a real ID field, otherwise we fall back on the idSpec id $idField = is_object($response) && isset($response->id) ? $response->id : (is_array($response) && isset($response['id']) ? $response['id'] : $requestItem->getUser()->getUserId($requestItem->getToken())); // construct <entry> blocks this record $feedEntry = $this->addNode($entry, 'entry'); $content = $this->addNode($feedEntry, 'content', '', array('type' => 'application/xml')); // Author node $author = $this->addNode($feedEntry, 'author'); $this->addNode($author, 'uri', $guid); $this->addNode($author, 'name', $authorName); // Special hoisting rules for activities if ($response instanceof Shindig_Activity) { $this->addNode($feedEntry, 'category', '', array('term' => 'status')); $this->addNode($feedEntry, 'updated', date(DATE_ATOM, $response->postedTime)); $this->addNode($feedEntry, 'id', 'urn:guid:' . $response->id); //FIXME should add a link field but don't have URL's available yet: // <link rel="self" type="application/atom+xml" href="http://api.example.org/activity/feeds/.../af3778"/> $this->addNode($feedEntry, 'title', strip_tags($response->title)); $this->addNode($feedEntry, 'summary', $response->body); // Unset them so addData doesn't include them again unset($response->postedTime); unset($response->id); unset($response->title); unset($response->body); } else { $this->addNode($feedEntry, 'id', 'urn:guid:' . $idField); $this->addNode($feedEntry, 'title', $requestType . ' feed entry for id ' . $idField); $this->addNode($feedEntry, 'updated', $updatedAtom); } // recursively add responseItem data to the xml structure $this->addData($content, $requestType, $response, self::$osNameSpace); } } else { // Single entry = Atom:Entry $entry = $doc->appendChild($doc->createElementNS(self::$nameSpace, "entry")); // Atom fields $this->addNode($entry, 'title', $requestType . ' entry for ' . $authorName); $author = $this->addNode($entry, 'author'); $this->addNode($author, 'uri', $guid); $this->addNode($author, 'name', $authorName); $this->addNode($entry, 'id', $guid); $this->addNode($entry, 'updated', $updatedAtom); $content = $this->addNode($entry, 'content', '', array('type' => 'application/xml')); // addData loops through the responseItem data recursively creating a matching XML structure $this->addData($content, $requestType, $data['entry'], self::$osNameSpace); } $xml = $doc->saveXML(); if ($responseItem->getResponse() instanceof RestfulCollection) { //FIXME dirty hack until i find a way to add multiple name spaces using DomXML functions $xml = str_replace('<feed xmlns="http://www.w3.org/2005/Atom">', '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:osearch="http://a9.com/-/spec/opensearch/1.1">', $xml); } echo $xml; }
/** * * @param ResponseItem $responseItem * @return array */ private function getErrorJson(ResponseItem $responseItem) { $error = array(); $error["code"] = $responseItem->getError(); $error["message"] = $responseItem->getErrorMessage(); return $error; }
function load_items() { $this->items = ResponseItem::all($this->ID); }
/** * Tests ResponseItem->setResponse() */ public function testSetResponse() { $expected = array('bar' => 2); $this->ResponseItem->setResponse($expected); $this->assertEquals($expected, $this->ResponseItem->getResponse()); }
<?php require '../loader.php'; ?> { "Data" : [ <?php $language_items = array(); if (isset($_GET['CompleteResponse'])) { $response = Response::random($_GET['Scope']); $response_items = ResponseItem::all($response->get_ID()); foreach ($response_items as $response_item) { $language_items[] = LanguageItem::load($response_item->get_LanguageItemId()); } } else { for ($i = isset($_GET['Start']) ? $_GET['Start'] : 0; $i < $_GET['End']; $i++) { $language_items[] = LanguageItem::random($_GET['Scope'], $i); } } $first = true; foreach ($language_items as $word) { if ($first) { $first = false; } else { echo ","; } ?> { "<?php echo $word->get_ID(); ?>
function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { echo json_encode($responseItem->getResponse()); }
private function outputError(ResponseItem $response) { $errorMessage = $response->getErrorMessage(); switch ($response->getError()) { case BAD_REQUEST: $code = '400 Bad Request'; break; case UNAUTHORIZED: $code = '401 Unauthorized'; break; case FORBIDDEN: $code = '403 Forbidden'; break; case FORBIDDEN: $code = '404 Not Found'; break; case NOT_IMPLEMENTED: $code = '501 Not Implemented'; break; case INTERNAL_ERROR: default: $code = '500 Internal Server Error'; break; } header("HTTP/1.0 {$code}", true); echo "{$code} - {$errorMessage}"; die; }
<?php function fail() { return '{ "success" : false }'; } require '../loader.php'; if (!$_POST['Data']) { return fail(); } $data = json_decode(str_replace('\\"', '"', $_POST['Data']), true); $response = new Response($data['Rating'], $data['Scope']); if (!$response->save()) { return fail(); } foreach ($data['Data'] as $language_item) { $language_item['Text'] = trim($language_item['Text']); $new_item = new LanguageItem($language_item['Text'], $language_item['Scope'], $language_item['Position'], $language_item['Type']); if (!$new_item->checkExistence() && !$new_item->save()) { return fail(); } $response_item = new ResponseItem($response->get_ID(), $new_item->get_ID()); if (!$response_item->save()) { return fail(); } } ?> { "success" : true }