PHP code example of wernerdweight / curler-bundle

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

    

wernerdweight / curler-bundle example snippets


    
    // config/bundles.php
    return [
        // ...
        WernerDweight\CurlerBundle\CurlerBundle::class => ['all' => true],
    ];

use WernerDweight\Curler\Curler;
use WernerDweight\Curler\Request;

class MyService
{
    /** Curler */
    private $curler;
     
    /**
     * @param Curler $curler
     */
    public function __construct(Curler $curler)
    {
        $this->curler = $curler;
    }
    
    public function myAction(): void
    {
        $request = (new Request())
            ->setEndpoint('https://some-website.tld')
            ->setMethod('POST')
            ->setPayload(['key' => 'value'])
            ->setHeaders(['Accept: text/html', 'Accept-Encoding: gzip'])
            ->setAuthentication('user', 'password')
        ;
        $response = $this->curler->request($request);
        echo $response->text();  // '<html>...</html>'
        var_dump($response->getMetaData()); // array of response metadata (content-type, status...)
    }
}