PHP code example of fancoders / php-api-request

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

    

fancoders / php-api-request example snippets

bash
  composer 
bash
  e Fancode\PhpApiRequest\ApiRequest;

  $baseUrl = 'https://jsonplaceholder.typicode.com';

  // Inisialisasi objek ApiRequest
  $apiRequest = new ApiRequest($baseUrl);

  // Contoh penggunaan metode GET
  $result = $apiRequest->get('/posts');
  // with query params
  $result = $apiRequest->get('/comments', ['postId' => 1]);


  // Contoh penggunaan metode POST
  $post = $apiRequest->post('/posts', [
   'userId' => 1,
   'title' => 'New Post',
   'body' => 'This is a new post.'
  ]);


  // Contoh penggunaan metode PUT
  $put = $apiRequest->put('/posts/1', [
   'title' => 'Updated Post',
   'body' => 'This post has been updated.'
  ]);


  // Contoh penggunaan metode PATCH
  $patch = $apiRequest->patch('/posts/1', [
   'title' => 'Updated Post',
   'body' => 'This post has been updated.'
  ]);


  // Contoh penggunaan metode DELETE
  $delete = $apiRequest->delete('/posts/1');

  // Tampilkan hasil
  echo "HTTP Status Code: {$result['status']}\n";
  echo "Response Body:\n";
  echo $result['response'] . "\n";