PHP code example of othercode / rest

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

    

othercode / rest example snippets


$api = new \OtherCode\Rest\Rest();
$api->configuration->url = "https://jsonplaceholder.typicode.com/";

$api = new \OtherCode\Rest\Rest(new \OtherCode\Rest\Core\Configuration(array(
    'url' => 'https://jsonplaceholder.typicode.com/',
)));

$response = $api->get("posts/1");

$api->addHeader('some_header','some_value');
$api->addHeaders(array('some_header' => 'some_value','other_header' => 'other_value'));

$api->configuration->addHeader('some_header','some_value');
$api->configuration->addHeaders(array('some_header' => 'some_value','other_header' => 'other_value'));

class CustomModule extends BaseModule
{
    public function run()
    {
        // do something
    }
}

$api->setModule('module_name','Module\Complete\Namespace','after');

$api->setDecoder("json");

class CustomDecoder extends BaseDecoder
{
	protected $contentType = 'application/json';

	protected function decode()
	{
		// decode $this->body
	}
}



try {

    $api = new \OtherCode\Rest\Rest(new \OtherCode\Rest\Core\Configuration(array(
        'url' => 'https://jsonplaceholder.typicode.com/',
        'httpheader' => array(
            'some_header' => 'some_value',
        )
    )));
    $api->setDecoder("json");
    
    $response = $api->get("posts/1");
    var_dump($response);
    
} catch (\Exception $e) {
    print "> " . $e->getMessage() . "\n"
}