PHP code example of abhilashpujari / php-restservice

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

    

abhilashpujari / php-restservice example snippets




 use RestService\RestService;

 $restService = new RestService();
 $response = $restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->get('/posts/1');
 

$restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->post('/posts');

$restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->put('/posts/1',
         [
             'id' => 1,
             'text' => 'Test'
         ]
     );

$restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->patch('/posts/1',
         [
             'id' => 1,
             'text' => 'Test'
         ]
     );

$restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->delete('/posts/1');

$restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->setIsFireAndForget(true)
     ->post('/posts');

$restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->setRequestHeaders([
         'auth' => 'somevalue'
     ])
     ->post('/posts');

$response = $restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->get('/posts/1', [], [], false);

 var_dump($response->getHeaders());
 var_dump($response->getBody());

 use RestService\RestService;

 $restService = new RestService();
 $response = $restService
     ->setEndpoint('https://jsonplaceholder.typicode.com')
     ->purge('/posts/1');
 
bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
bash
php composer.phar 
bash
php composer.phar update