PHP code example of ahmard / guzwrap

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

    

ahmard / guzwrap example snippets


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();

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();

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

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();

  \sleep($_GET['sleep']);
  echo $_GET['sleep'];
  

  use Guzwrap\Request;
  use Psr\Http\Message\ResponseInterface;
  
  $promise = Request::get('localhost:8002')
      ->query('wraps', 'guzzlehttp')
      ->query('sleep', 2)
      ->execAsync();
  
  $promise->then(function (ResponseInterface $response){
      var_dump($response->getBody()->getContents());
  });
  
  $promise2 = Request::get('localhost:8002')
      ->query('name', 'guzwrap')
      ->query('sleep', 1)
      ->execAsync();
  
  $promise2->then(function (ResponseInterface $response){
      var_dump($response->getBody()->getContents());
  });
  
  $promise->wait();
  $promise2->wait();
  

  use Guzwrap\Request;  

  $promise1 = Request::get('localhost:8002')->execAsync();
  $promise2 = Request::get('localhost:8002')->execAsync();
  
  $responses = Request::concurrent($promise1, $promise2)->unwrap();
  
  echo $responses[0]->getStatusCode() . PHP_EOL;
  echo $responses[1]->getReasonPhrase() . PHP_EOL;
  

  use Guzwrap\Request;
  use Guzwrap\Wrapper\Pool;
  use GuzzleHttp\Exception\RequestException;
  use GuzzleHttp\Psr7\Response;
  use Psr\Http\Message\ResponseInterface;
  
  each successful response
      });
      $pool->rejected(function (RequestException $reason, $index) {
          // this is delivered each failed request
      });
  
      $pool->requests(function ($total) {
          $uri = 'http://127.0.0.1:8002';
          for ($i = 0; $i < $total; $i++) {
              yield new \GuzzleHttp\Psr7\Request('GET', $uri);
          }
      });
  });
  
  $promise = $pool->promise();
  
  $promise->then(function (ResponseInterface $response){
      echo "{$response->getStatusCode()} @ {$response->getReasonPhrase()}\n";
  });
  
  $promise->wait();
  

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)'));

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

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

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);
        };
    };
});

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

Client
    {
        return new Client();
    }

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

$client = Client::create()
    ->get('localhost:8002')
    ->withCookie()
    ->boom();
bash
  php -S localhost:8002 index.php
  
bash
  php test.php