1. Go to this page and download the library: Download nimbly/activeresource library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use ActiveResource\Model;
/**
* Because the class name is "Users", ActiveResource assumes the API endpoint for this resource is "users".
*/
class Users extends Model
{
/**
* The single user object can be found at $.data.user
*
* Sample response payload:
*
* {
* "data": {
* "user": {
* "id": "1",
* "name": "Foo Bar",
* "email": "[email protected]"
* }
* }
* }
*
*
*/
protected function parseFind($payload)
{
return $payload->data->user;
}
protected function parseAll($payload)
{
return $payload->data->users;
}
}
/**
* Because the class name is "Posts", ActiveResource assumes the API endpoint for this resource is "posts".
*/
class Posts extends Model
{
// Manually set the endpoint for this resource. Now ActiveResource will hit "blogs" when making calls
// from this model.
protected $resourceName = 'blogs';
/**
* A blog post has an author object embedded in the response that
* maps to a User object.
*/
protected function author($data)
{
return $this->
$user = new User;
$user->name = 'Brent Scheffler';
$user->email = '[email protected]';
$user->save();
$post = new Posts;
$post->title = 'Blog post';
$post->body = 'World\'s shortest blog post';
$post->author_id = $user->id;
$post->save();
// Update the author (user)
$post->author->email = '[email protected]';
// Oops, save failed... Wonder what happened.
if( $post->author->save() == false )
{
// Looks like that email address is already being used
$code = $post->getResponse()->getStatusCode(); // 409
$error = $post->getResponse()->getStatusPhrase(); // Conflict
}
// Get user ID=1
$user = User::find(1);
// Update the user
$user->status = 'inactive';
$user->save();
// Get all the users
$users = Users::all();
// Pass in some query params to find only active users
$users = Users::all(['status' => 'active']);
// Delete user ID=1
$user->destroy();
// Get the response code
$statusCode = $user->getResponse()->getStatusCode(); // 204 No Content
// Pass in a specific header for this call
$post = Posts::all([], ['X-Header-Foo' => 'Bar']);
// Get blog post ID=1
$post = Posts::find(1);
// Get all comments through Posts resource. The effective query would be GET#/blogs/1/comments
$comments = Comments::allThrough($post);
// Or...
$comments = Comments::allThrough("blogs/1");
class Response extends \ActiveResource\ResponseAbstract
{
public function decode($payload)
{
return json_decode($payload);
}
public function isSuccessful()
{
return $this->getStatusCode() < 400;
}
public function getMeta()
{
return $this->getPayload()->meta;
}
public function getEnvelope()
{
return $this->getPayload()->envelope;
}
}
class Users extends \ActiveResource\Model
{
}
class Comments extends \ActiveResource\Model
{
public function author($data)
{
return $this->
class Posts extends \ActiveResource\Model
{
public function author($data)
{
return $this->data);
}
/**
* You can find the blog post data in $.data.post in the payload
*/
protected function parseFind($payload)
{
return $payload->data->post;
}
/**
* You can find the collection of post data in $.data.posts in the payload
*/
protected function parseAll($payload)
{
return $payload->data->posts;
}
}
$posts = Posts::find(7);
class Authorize implements LayerInterface
{
/**
*
* @param \ActiveResource\Request $object
*/
public function peel($object, \Closure $next)
{
// Add a query param to the URL (&foo=bar)
$object->setQuery('foo', 'bar');
// Do some HMAC authorization logic here
// ...
// ...
// Now add the HMAC headers
$object->setHeader('X-Hmac-Timestamp', $timestamp);
$object->setHeader('Authorization', "HMAC {$hmac}");
// Send the request off to the next layer
$response = $next($object);
// Now let's slip in a spoofed header into the response object
$response->setHeader('X-Spoofed-Response-Header', 'Foo');
// How about we completely change the response status code?
$response->setStatusCode(500);
// Return the response
return $response;
}
}