PHP code example of calderawp / http

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

    

calderawp / http example snippets


$http = \caldera()->getHttp(); 

        use GuzzleHttp\Handler\MockHandler;
        use GuzzleHttp\HandlerStack;
        use GuzzleHttp\Psr7\Response;
        
        //...
        
        $mockResponse = new Response(200, ['X-HELLO' => 'ROY'],
			json_encode(['messageFromServer' => 'Everything Is An Illusion.']));
		$mock = new MockHandler([
			$mockResponse,
		]);
		$handler = HandlerStack::create($mock);
		$client = new Client(['handler' => $handler]);
		\caldera()->getHttp()->setClient($client);

$apiRequest = new \calderawp\caldera\Http\Request();
$apiRequest->setHttpMethod('POST'); //how to dispatch 
$apiRequest->setParams([ 'stateOfMind' => 'Super Chill' ); //Request body
$apiRequest->setHeaders([ 'X-CONTENT-TYPE' => 'application/json' ] ); //Request headers

try {
    $response = \caldera()
        ->getHttp()
        ->send($apiRequest, $url);
} catch (\Exception $e) {
    throw  $e;
}

$status = $response->getHeaders(); //All headers returned by remote API
$status = $response->getStatus(); //status code returned
$body = $response->getData();// body of remote request response