json-api/client

There is no license information available for the latest version (dev-master) of this package.

Simple JSON RESTful api client library

dev-master 2020-05-28 21:25 UTC

This package is auto-updated.

Last update: 2024-10-19 22:22:31 UTC


README

Example:

    require_once 'vendor/autoload.php';
    
    class JSONPlaceholder extends \JsonAPI\Model {
        use \JsonAPI\Resource;
    
        protected $endpoint = 'https://jsonplaceholder.typicode.com/todos';
    
        public static function FromArray(array $data)
        {
            $object = new self;
    
            // @todo Schema validation here
            $object->userId = $data['userId'];
            $object->id = $data['id'];
            $object->title = $data['title'];
    
            return $object;
        }
    
        /**
         * Get object by ID
         *
         * @param $id
         * @return JSONPlaceholder
         * @throws \JsonAPI\Client\Exception
         */
        public function get($id)
        {
            return $this->request($id, 'GET', [], self::class);
        }
    
        public function getId() : string
        {
            return $this->id;
        }
    
        public function getUserId() : string
        {
            return $this->userId;
        }
    
        public function getTitle() : string
        {
            return $this->title;
        }
    }
    
    
    $test = new JSONPlaceholder;
    $object = $test->setAdapter(new \JsonAPI\Client\Adapter\Curl())->get(1);
    
    echo $object->getId().' - '.$object->getTitle(); // will echo '1 - delectus aut autem'