PHP code example of kwhat / requestful

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

    

kwhat / requestful example snippets


$factory = new Nyholm\Psr7\Factory\Psr17Factory();
$client = new Requestful\Http\Client($factory);
$request = $factory->createRequest(
    "GET", 
    "https://samples.openweathermap.org/data/2.5/weather?q=Los%20Angeles"
);

// Send a synchronous request
$response = $client->sendRequest($request);

var_dump(
    $response->getStatusCode(), // int(200)
    $response->getHeaderLine("content-type"), // string(30) "application/json; charset=utf8"
    $response->getBody() // string(459) "{"coord": {...}, "weather": {...}, ...}"
);

// Send an asynchronous request
$promise = $client->sendRequestAsync($request)
    ->then(function (Psr\Http\Message\ResponseInterface $response): string {
        return "Success: {$response->getStatusCode()}";
    });

var_dump($promise->wait()); // string(12) "Success: 200"