PHP code example of artesaos / restinga

1. Go to this page and download the library: Download artesaos/restinga 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/ */

    

artesaos / restinga example snippets


 namespace Artesaos\DigitalOcean;

use Artesaos\Restinga\Authorization\Bearer;
use Artesaos\Restinga\Service\Descriptor;

class DigitalOceanDescriptor extends Descriptor
{
    // service alias
    protected $service = 'digital-ocean';

    // api prefix
    protected $prefix = 'https://api.digitalocean.com/v2';

    // how to authenticate on the api
    public function authorization()
    {
        return new Bearer('your-token-here');
    }
}


use Artesaos\DigitalOcean\DigitalOceanDescriptor;
use Artesaos\Restinga\Container;

Container::register(new DigitalOceanDescriptor());

 namespace Artesaos\DigitalOcean\Resource;

use Artesaos\Restinga\Data\Resource;
use Artesaos\Restinga\Http\Format\Receive\ReceiveJson;
use Artesaos\Restinga\Http\Format\Receive\ReceiveJsonErrors;
use Artesaos\Restinga\Http\Format\Send\SendJson;

class Droplet extends Resource
{
    // send & receive formats
    use ReceiveJson;
    use SendJson;

    // errors format
    use ReceiveJsonErrors;

    // the service to use (defined on the descriptor)
    protected $service = 'digital-ocean';

    // api resource to consume
    protected $name = 'droplets';

    // resource identifier
    protected $identifier = 'id';

    // resource collection root
    protected $collection_root = 'droplets';

    // resource single item root
    protected $item_root = 'droplet';
}



use Artesaos\DigitalOcean\Resource\Droplet;

$droplet = new Droplet();
$droplet->name = 'server.restinga.dev';
$droplet->region = 'nyc3';
$droplet->size = '512mb';
$droplet->image = 'ubuntu-14-04-x64';

$saved = $droplet->save();

if ($saved) {
    echo $droplet->id; // 4242424
} else {
    foreach ($droplet->errors->all() as $code => $error) {
        echo $code . ": " . $error . "\n";
    }
}