PHP code example of haspadar / carl

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

    

haspadar / carl example snippets


$response = new CurlClient()->outcome(
    new GetRequest('https://httpbin.org/get')
)->response();

echo $response->body(); 

$client = new CurlClient();

$requests = [
    new GetRequest('https://httpbin.org/status/200'),
    new GetRequest('https://httpbin.org/status/404'),
];

$outcomes = $client->outcomes($requests, new class implements Reaction {
    public function onSuccess(Request $request, Response $response): void
    {
        echo "Success: " . $response->body() . "\n";
    }
    public function onFailure(Request $request, string $error): void
    {
        echo "Failure: " . $error . "\n";
    }
});
+Note: CurlClient returns outcomes in completion order (not request order).


new FakeClient(new Cycle([
    new AlwaysSuccessful(new SuccessResponse("OK")),
    new AlwaysFails("network error"),
]))->outcomes(
    [
        new GetRequest('https://example.com/a'),
        new GetRequest('https://example.com/b'),
    ], 
    new OnSuccessResponse(
        fn (Response $response) => print $response->body()
    )
);
// Sequence: OK, error, OK, error, ...

$response = new CurlClient()->outcome(
    new GetRequest('https://httpbin.org/get')
)->response();

echo $response->body();