Home

Awesome

Guzwrap

Guzwrap is an object-oriented wrapper around GuzzleHttp. <br/> This project is founded to make sending request with Guzzle easier and enjoyable.

Supported PHP Versions

Guzwrap require PHP >= 7.4 or >= 8.0.

Installation

Make sure you have Composer installed.

composer require ahmard/guzwrap

Usage

use Guzwrap\Request;

//simple request
$result = Request::get('http://localhost:8002')->exec();

//with authentication
Request::get('http://localhost:8002')
    ->auth('username', 'password')
    ->exec();
use Guzwrap\Request;

$instance = Request::create();
//Do something...
use Guzwrap\Request;

Request::create()->get('http://localhost:8002')
    ->withCookie()
    //or use cookie file
    ->withCookieFile('path/to/file')
    //use cookie session
    ->withCookieSession('session_name')
    //use array too
    ->withCookieArray([
        'first_name' => 'Jane',
        'other_names' => 'Doe'
    ], 'localhost')
    //Use single cookie across requests
    ->withSharedCookie();
use Guzwrap\Request;
use Guzwrap\Wrapper\Redirect;

Request::get('http://localhost:8002')
    ->redirects(function(Redirect $redirect){
        $redirect->max(5);
        $redirect->strict();
        $redirect->referer('http://goo.gl');
        $redirect->protocols('http');
        $redirect->trackRedirects();
        $redirect->onRedirect(function(){
            echo "Redirection detected!";
        });
    })->exec();
use Guzwrap\Request;
use Guzwrap\Wrapper\Header;

Request::get('http://localhost:8002')
    ->header(function(Header $header){
        $header->add('hello', 'world');
        $header->add('planet', 'earth');
    })
    ->exec();
use Guzwrap\Request;

Request::get('https://google.com')
    ->query('q', 'Who is jane doe')
    ->exec();
use Guzwrap\Request;
use Guzwrap\Wrapper\Form;

Request::uri('http://localhost:8002')
    ->post(function(Form $form){
        $form->field('first_name', 'Jane');
        $form->field('last_name', 'Doe');
    })
    ->exec();

//Post with multipart data
Request::uri('http://localhost:8002')
  ->post(function(Form $form){
      $form->method('post');
      $form->field('full_name', 'Jane Doe');
      $form->file('avatar', 'C:\jane_doe.jpg');
  })->exec();

You can use RequestInterface::form() method

Note: If you did not set form method to post, all your input fields will be treated as url queries.

use Guzwrap\Request;
use Guzwrap\Wrapper\Form;
use Guzwrap\Wrapper\File;

Request::form(function (Form $form){
    $form->method('get'); //You can use any http method here
    $form->action('localhost:8002');
    $form->field('name', 'Guzwrap');
})->exec();

//Send file with custom information
Request::form(function(Form $form){
    $form->method('post');
    $form->action('http://localhost:8002');
    $form->field('full_name', 'Jane Doe');
    $form->file(function(File $file){
        $file->field('avatar');
        $file->path('C:\jane_doe.jpg');
        $file->name('John_doe.gif');
    });
})->exec();

More Request usage

use Guzwrap\Request;
use Guzwrap\UserAgent;

//Basic usage
$request = Request::query('artist', 'Taylor Swift')
    ->useData([
        'headers' => [
            'pass' => 'my-random-pass',
            'user-agent' => 'My Custom Useragent',
        ],
        'query' => [
            'action' => 'create'
        ]       
    ])->exec();

//User other request's data
$request1 = Request::userAgent(UserAgent::FIREFOX)
    ->query([
        'username' => 'Ahmard',
        'realm' => 'admin'
    ]);

$realRequest = Request::useData($request1->getData());
use Guzwrap\Request;
use Guzwrap\UserAgent;

$request1 = Request::query('username', 'Ahmard');

$request2 = Request::query('language', 'PHP')
    ->userAgent(UserAgent::CHROME)
    ->allowRedirects(false);

$realRequest = Request::useRequest($request1, $request2); //Has request 1 and request 2 data

Asynchronous Operations

use Guzwrap\Request;
use Psr\Http\Message\ResponseInterface;

$promise = Request::get('localhost:8002')
    ->query('wraps', 'guzzlehttp')
    ->query('name', 'guzwrap')
    ->execAsync();

$promise->then(function (ResponseInterface $response){
    var_dump($response->getBody()->getContents());
});

$promise->wait();

UserAgent

We provide custom user agents to help send request easily.

use Guzwrap\Request;
use Guzwrap\UserAgent;

Request::userAgent(UserAgent::CHROME);

//Choose specific useragent index from array
Request::userAgent(UserAgent::CHROME, '1');

//Choose sub-useragent
Request::userAgent(UserAgent::CHROME, '9.1');
use Guzwrap\UserAgent;

$userAgents = UserAgent::init()->getAvailable();
use Guzwrap\UserAgent;

$randomUA = UserAgent::init()->getRandom();
use Guzwrap\UserAgent;

UserAgent::init()->addFile('/path/to/user-agents.json');
use Guzwrap\UserAgent;
use Guzwrap\Request;

$request = Request::userAgent(UserAgent::raw('Browser 1.0 (Windows NT 10.0; Win64; x64)'));

Stack

Manipulating Guzzle StackHandler

use Guzwrap\Request;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;

Request::stack(function (HandlerStack $stack){
    $stack->setHandler(new CurlHandler());
    //Do something here
});

Middleware

Adding middleware to GuzzleHttp requests

use Guzwrap\Request;
use Psr\Http\Message\RequestInterface;

/**
 * An example of middleware that add header to requests
 * @link https://docs.guzzlephp.org/en/stable/handlers-and-middleware.html
 */
Request::middleware(function(){
    return function (callable $handler){
        return function (
            RequestInterface $request,
            array $options
        ) use ($handler){
            $request = $request->withHeader('X-Guzwrap-Version', 'V2');
            return $handler($request, $options);
        };
    };
});

Extending Guzwrap

use Guzwrap\Wrapper\Guzzle;
use Psr\Http\Message\ResponseInterface;

require 'vendor/autoload.php';

class Client extends Guzzle
{
    public static function create(): Client
    {
        return new Client();
    }

    public function boom(): ResponseInterface
    {
        echo "Executing request...\n";
        return parent::exec();
    }
}

$client = Client::create()
    ->get('localhost:8002')
    ->withCookie()
    ->boom();

Testing

Rust PHP built-in server before running the test

php -S localhost:8002 -t tests/raw/

Run the test

composer test

Enjoy 😎