PHP code example of radiergummi / wander

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

    

radiergummi / wander example snippets


$responseBody = (new Wander())
    ->patch('https://example.com')
    ->withQueryParameter('foo', 'bar')
    ->withBasicAuthorization('User', 'Pass')
    ->withHeaders([
        Header::ACCEPT => MediaType::APPLICATION_JSON,
    ])
    ->withoutHeader(Header::USER_AGENT)
    ->withBody([ 'test' => true ])
    ->asJson()
    ->run()
    ->getBody()
    ->getContents();

$client = new Wander();
$response = $client
    ->get('https://example.com')
    ->run();

$client = new Wander();
$response = $client
    ->createContext(Method::GET, 'https://example.com')
    ->run();

$client = new Wander();
$request = new Request(Method::GET, 'https://example.com');
$response = $client->request($request);

$client = new Wander();
$response = $client
    ->post('https://example.com')
    ->withBody([
        'anything' => true
    ])
    ->asJson()
    ->run();

$client = new Wander();
$response = $client
    ->post('https://example.com')
    ->withBody(Stream::create('/home/moritz/large.mp4'))
    ->run();

$client = new Wander();
$response = $client
    ->get('https://example.com')
    ->run()
    ->getParsedBody();

try {
    $request->run();
} catch (UnauthorizedException | ForbiddenException $e) {
    $this->refreshAccessToken();

    return $this->retry();
} catch (GoneException $e) {
    throw new RecordDeletedExeption(
        $e->getRequest()->getUri()->getPath()
    );
} catch (BadRequestException $e) {
    $responseBody = $e->getResponse()->getBody()->getContents();
    $error = json_decode($responseBody, JSON_THROW_ON_ERROR);
    $field = $error['field'] ?? null;
    
    if ($field) {
        throw new ValidatorException("Failed to validate {$field}");
    }
    
    throw new UnknownException($error);
} catch (WanderException $e) {

    // Simply catch all others
    throw new RuntimeException(
        'Server returned an unknown error: ' .
        $e->getResponse()->getBody()->getContents()
    );
}

$driver = new StreamDriver();
$driver->setTimeout(3000); // 3000ms / 3s

$client = new Wander($driver);

$driver = new StreamDriver();
$driver->followRedirects(false);

$client = new Wander($driver);

$driver = new StreamDriver();
$driver->setMaximumRedirects(3);

$client = new Wander($driver);

$client = new Wander();
$client->addSerializer('your/media-type', new CustomSerializer());

$driver = new class implements DriverInterface {
    public function sendRequest(RequestInterface $request): ResponseInterface
    {
      // TODO: Implement sendRequest() method.
    }
    
    public function setResponseFactory(ResponseFactoryInterface $responseFactory): void
    {
      // TODO: Implement setResponseFactory() method.
    }
};

$client = new Wander($driver);

new Wander(
    DriverInterface $driver = null,
    ?RequestFactoryInterface $requestFactory = null,
    ?ResponseFactoryInterface $responseFactory = null
)

get(UriInterface|string $uri): Context

post(UriInterface|string $uri, ?mixed $body = null): Context

put(UriInterface|string $uri, ?mixed $body = null): Context

patch(UriInterface|string $uri, ?mixed $body = null): Context

delete(UriInterface|string $uri, ?mixed $body = null): Context

head(UriInterface|string $uri, ?mixed $body = null): Context

options(UriInterface|string $uri, ?mixed $body = null): Context

createContext(string $method, UriInterface|string $uri): Context

createContextFromRequest(RequestInterface $request): Context

request(RequestInterface $request): ResponseInterface

new Context(
    HttpClientInterface $client,
    RequestInterface $request
)