public function indexAction() { $response = new ApiResponse(); if ($this->request->isGet()) { $limit = (int) $this->request->get('limit'); $page = (int) $this->request->get('page'); $questions = Tags::find(); $paginator = new PaginatorModel(array("data" => $questions, "limit" => $limit, "page" => $page)); $page = $paginator->getPaginate(); $response->setResponse($page->items, count($questions)); return $response; } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
public function newAction() { $response = new ApiResponse(); if ($this->request->isPost()) { $answer = new Answers(); $answer->id = uniqid(); $answer->content = $this->request->getPost('content'); $answer->users_id = $this->request->getPost('users_id'); $answer->questions_id = $this->request->getPost('questions_id'); if ($this->request->hasFiles() == true) { $baseLocation = 'files/'; foreach ($this->request->getUploadedFiles() as $file) { $photos = new Photos(); $unique_filename = $answer->id; $photos->size = $file->getSize(); $photos->original_name = $file->getName(); $photos->file_name = $unique_filename; $photos->extension = $file->getExtension(); $location = $baseLocation . $unique_filename . "." . $file->getExtension(); $photos->public_link = $location; try { if (!$photos->save()) { $response->setResponseError($photos->getMessages()); } else { //Move the file into the application $file->moveTo($location); $answer->photo = $photos->public_link; } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } } try { if ($answer->save() == false) { $response->setResponseError($answer->getMessages()); } else { $response->setResponse($answer->id); } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
public function indexAction() { $response = new ApiResponse(); if ($this->request->isGet()) { $limit = $this->request->get('limit'); $page = $this->request->get('page'); $questions = Questions::find(array("order" => "created_at DESC")); $paginator = new PaginatorModel(array("data" => $questions, "limit" => $limit, "page" => $page)); $page = $paginator->getPaginate(); $res = []; foreach ($page->items as $item) { $user = Users::findFirstById($item->users_id); $item->user_name = $user->username; $item->user_avatar = $user->avatar; $res[] = $item; } $response->setResponse($res, count($questions)); return $response; } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
public function loginAction() { $response = new ApiResponse(); if ($this->request->isPost()) { $email = $this->request->getPost('email'); $password = $this->request->getPost('password'); // Check if the user exist $user = Users::findFirstByEmail($email); if ($user == false) { $response->setResponseError('Wrong email/password combination 1'); return $response; } // Check the password if (!$this->security->checkHash($password, $user->password)) { $response->setResponseError('Wrong email/password combination'); return $response; } $response->setResponse(array('id' => $user->id, 'username' => $user->username, 'email' => $user->email, 'avatar' => $user->avatar)); } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
private function performGet($Url) { $res = wp_remote_get($Url); //echo '<pre>';var_dump( wp_remote_retrieve_headers($res)); if (is_wp_error($res)) { // ( var_dump( ($res->errors) )); if (count($res->errors) == 1) { // If there was only 1 error let's see if I can deal with it $error = array_keys($res->errors); $error = $error[0]; switch ($error) { case 'http_request_failed': throw new HttpRequestFailedException($Url); default: // WordPress had some error that I do not know about throw new UnknownErrorException($res); } echo '1 error'; } else { // WordPress had some error that I do not know about throw new UnknownErrorException($res); } die; } else { $response = new ApiResponse(); $response->setHttpCode(wp_remote_retrieve_response_code($res)); $response->setHeaders(wp_remote_retrieve_headers($res)); $response->setResponse(wp_remote_retrieve_body($res)); if ($response->getHttpCode() == '400') { // 400 Bad request when there was a problem with the request throw new BadRequestException($Url, $response); } else { if ($response->getHttpCode() == '401') { // 401 Unauthorized when you don't provide a valid key throw new UnauthorizedRequestException(); } else { if ($response->getHttpCode() == '500') { // 500 Internal Server Error throw new InternalServerErrorException(); } } } return $response; } }
public function detailAction() { $response = new ApiResponse(); if ($this->request->isGet()) { $offers_id = $this->request->get('offers_id'); $offer = Offers::findFirstById($offers_id); if ($offer == false) { $response->setResponseError('No offer found!'); return $response; } else { $response->setResponse($offer, 1); return $response; } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }