PHP code example of arthof / fake-api-client

1. Go to this page and download the library: Download arthof/fake-api-client 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/ */

    

arthof / fake-api-client example snippets



use FakeApiClient\Client;
$Client = new Client();
$postId = 1;
$userId = 2;

//[GET] fetch single post 
$post = $Client->Posts->Fetch($postId);

//[GET] fetch all posts
$posts = $Client->Posts->FetchAll();

//[GET] fetch posts by user
$postsByUser = $Client->Posts->FetchByUser($userId);

//[GET] fetch post comments
$postComments = $Client->Posts->FetchComments($postId);

//[POST] create post
$createPost = [
    'title' => 'Lorem ipsum',
    'body' => 'Sample text',
    'userId' => 3,
];
$newPost = $Client->Posts->Create($createPost);

//[PUT] replace post
$postId = 74;
$replacePost = [
    'id' => 74,
    'title' => 'Lorem ipsum',
    'body' => 'Sample text',
    'userId' => 3,
];
$replacedPost= $Client->Posts->Replace($postId, $replacePost);

//[PATCH] update post
$postId = 74;
$replacePost = [
    'title' => 'Winnie the Pooh',
];
$updatedPost = $Client->Posts->Update($postId, $replacePost);

//[DELETE] delete post
$deletedPost = $Client->Posts->Delete($postId);

use FakeApiClient\Client;
$httpClient = new DifferentHttpClient();
$Client = new Client('https://new-api-url/', $httpClient);


//src/FakeApiClient/Todos.php
namespace FakeApiClient;

use FakeApiClient\Resources;

class Todos extends Resources
{
    const TYPE = 'todos';
}

//src/FakeApiClient/Client.php
namespace FakeApiClient;

class Client
{
    public $Posts;
    public $Todos;

    public function __construct(string $baseUrl=null, IHttpMethods $httpClient=null)
    {
        $this->Posts = new Posts($baseUrl, $httpClient);
        $this->Todos = new Todos($baseUrl, $httpClient);
    }
}

use FakeApiClient\Client;
$Client = new Client();

//[GET] fetch all todos
$todos = $Client->Todos->FetchAll();