Ejemplo n.º 1
0
 /**
  * Fetch the index data
  *
  * @throws Requests_Exception Failed to retrieve the index
  * @throws Exception Failed to decode JSON
  * @return boolean Was it successful?
  */
 public function fetch()
 {
     $response = $this->api->get(WPAPI::ROUTE_INDEX);
     $response->throw_for_status();
     $data = json_decode($response->body, true);
     $has_error = function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE;
     if (!$has_error && $data === null || $has_error) {
         throw new Exception($response->body);
     }
     $this->data = $data;
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Get a single post
  *
  * @throws Requests_Exception Failed to retrieve the post
  * @throws Exception Failed to decode JSON
  * @param int $id Post ID
  * @return WPAPI_Post
  */
 public function get($id)
 {
     $url = sprintf(WPAPI::ROUTE_POST, $id);
     $response = $this->api->get($url);
     $response->throw_for_status();
     $data = json_decode($response->body, true);
     $has_error = function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE;
     if (!$has_error && $data === null || $has_error) {
         throw new Exception($response->body);
     }
     return new WPAPI_Post($this->api, $data);
 }
Ejemplo n.º 3
0
 /**
  * Get the current user
  *
  * @throws Requests_Exception Failed to retrieve the user
  * @throws Exception Failed to decode JSON
  * @return WPAPI_User
  */
 public function getCurrent($will_edit = false)
 {
     $url = WPAPI::ROUTE_USER_CURRENT;
     if ($will_edit) {
         $url .= '?context=edit';
     }
     $response = $this->api->get($url);
     $response->throw_for_status();
     $data = json_decode($response->body, true);
     $has_error = function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE;
     if (!$has_error && $data === null || $has_error) {
         throw new Exception($response->body);
     }
     return new WPAPI_User($this->api, $data);
 }