PHP code example of fgsl / rest

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

    

fgsl / rest example snippets


use Fgsl\Rest\Rest;

$rest = new Rest();

/**
     * Method to make a HTTP GET request
     * @param $headers array
     * @param $url string
     * @param $expectedCode string | integer | array
     */
    public function doGet($headers,$url,$expectedCode, $data = [],$verbose=false)

/**
     * Method to make a HTTP POST request
     * @param $data array
     * @param $headers array
     * @param $url string
     * @param $expectedCode string | integer | array
     */
    public function doPost($data,$headers,$url,$expectedCode, $verbose=false)

    /**
     * Method to make a HTTP PUT request
     * @param $data array
     * @param $headers array
     * @param $url string
     * @param $expectedCode string | integer | array
     */
    public function doPut($data,$headers,$url,$expectedCode, $verbose=false) {

/**
     * Method to make a HTTP DELETE request
     * @param $headers array
     * @param $url string
     * @param $expectedCode string | integer | array
     * @param $data array
     * @param $verbose boolean
     */
    public function doDelete($headers,$url,$expectedCode, $data = [],$verbose=false)

/**
     * Method to make a HTTP PATCH request
     * @param $data array
     * @param $headers array
     * @param $url string
     * @param $expectedCode string | integer | array
     */
    public function doPatch($data,$headers,$url,$expectedCode, $verbose=false)

/**
     * Method to make a HTTP PATCH request
     * @param $data array
     * @param $headers array
     * @param $url string
     * @param $expectedCode string | integer | array
     */
    public function doPatch($data,$headers,$url,$expectedCode, $verbose=false)

public function testGet()
    {
        $rest = new Rest();
        $this->assertTrue(is_object($rest));
        
        @$response = $rest->doGet([],'http://www.horalegalbrasil.mct.on.br/SincronismoPublico.html',200);
        
        $this->assertStringContainsString('Sincronismo', $response);

        @$response = $rest->doGet([],'http://www.horalegalbrasil.mct.on.br/SincronismoPublico.html',500);
        
        $this->assertEquals(1,count($rest->requestErrors));

        $this->assertEquals(2,$rest->requestCounter);
    }

public function testPost()
    {
        $rest = new Rest();

        $data = [
            'name' => 'morpheus',
            'job' => 'leader'
        ];
        
        @$response = $rest->doPost($data, [],'https://reqres.in/api/users',201);
        
        $this->assertStringContainsString('createdAt', $response);
    }

    public function testPut()
    {
        $rest = new Rest();

        $data = [
            'name' => 'morpheus',
            'job' => 'general'
        ];
        
        @$response = $rest->doPut($data, [],'https://reqres.in/api/users/2',201);
        
        $this->assertStringContainsString('updatedAt', $response);
    }

public function testPatch()
    {
        $rest = new Rest();

        $data = [
            'name' => 'morpheus',
            'job' => 'zion resident'
        ];
        
        @$response = $rest->doPatch($data, [], 'https://reqres.in/api/users/2', 200);
        
        $this->assertStringContainsString('updatedAt', $response);
    }

public function testDelete()
    {
        $rest = new Rest();
        
        @$response = $rest->doDelete([],'https://reqres.in/api/users/2',204);

        $this->assertEquals(0,count($rest->requestErrors));
    }
}

        @$response = $rest->doGet([],'http://www.horalegalbrasil.mct.on.br/SincronismoPublico.html',[200,201]);
        
        $this->assertStringContainsString('Sincronismo', $response);