PHP code example of nimbly / activeresource

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/ */

    

nimbly / activeresource example snippets



    $options = [
        Connection::OPTION_BASE_URI => 'https://someapi.com/v1/',
        Connection::OPTION_DEFAULT_HEADERS => [
            'Authorization' => 'Bearer MYAPITOKEN',
        ]
    ];
    
    $connection = new Connection($options);

    
    ConnectionManager::add('default', $connection);


    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");


    $options = [
        Connection::OPTION_BASE_URI => 'http://api.someurl.com/v1/',
        Connection::OPTION_UPDATE_METHOD => 'patch',
        Connection::OPTION_UPDATE_DIFF => true,
        Connection::OPTION_RESPONSE_CLASS => \My\Custom\Response::class,
        Connection::OPTION_MIDDLEWARE => [
            \My\Custom\Middleware\Authorize::class,
            \My\Custom\Middleware\Headers::class,
        ]
    ];
    
    $connection = new \ActiveResource\Connection($options);


    ConnectionManager::add('yourConnectionName', $connection);


    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;
        }
    }

    $connection = new Connection([
        Connection::OPTION_BASE_URI => 'https://someurl.com/v1/',
        Connection::OPTION_LOG => true,
    ]);
    
    ConnectionManager::add('yourConnectionName', $connection);
    
    $post = Post::find(12);

    $connection = ConnectionManager::get('yourConnectionName');
    $log = $connection->getLog();
    
        // Or...

    
    $post->getConnection()->getLog();

       // Or...
    
    Post::connection()->getLog();


    $user = User::find(123);


    $users = User::all();


    $user = new User;
    $user->name = 'Test User';
    $user->email = '[email protected]';
    $user->save();


    $user = User::find(123);
    $user->status = 'INACTIVE';
    $user->save();


    $user = User::find($id);
    $user->fill([
        'name' => 'Buckley',
        'email' => '[email protected]',
    ]);
    $user->save();


    $user = User::find($id);
    $user->destory();
    
    // Or...
    
    User::delete($id);


        $options = [
            Connection::OPTION_BASE_URI => 'http://myapi.com/v2/',
            Connection::OPTION_DEFAULT_HEADERS => [
                'Authorization' => 'Bearer MYAPITOKEN',
            ],
        ];

        $connection = new Connection($options);


    $connection = ConnectionManager::get('yourConnectionName');
    $request = $connection->buildRequest('post', '/some/oddball/endpoint', ['param1' => 'value1'], ['foo' => 'bar', 'fox' => 'sox'], ['X-Custom-Header', 'Foo']);
    $response = $connection->send($request);