PHP code example of fofx / guzzle-middleware

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

    

fofx / guzzle-middleware example snippets



use FOfX\GuzzleMiddleware\MiddlewareClient;

$client = new MiddlewareClient();
$response = $client->makeRequest('GET', 'http://httpbin.org/redirect/2');
print_r($client->getAllTransactions());

Array
(
    [0] => Array
        (
            [request] => Array
                (
                    [method] => GET
                    [url] => http://httpbin.org/redirect/2
                    [headers] => {"User-Agent":["GuzzleHttp\/7"],"Host":["httpbin.org"]}
                    [body] => 
                    [protocol] => 1.1
                    [target] => /redirect/2
                )

            [response] => Array
                (
                    [statusCode] => 302
                    [headers] => {"Date":["Mon, 13 Jan 2025 19:03:26 GMT"],"Content-Type":["text\/html; charset=utf-8"],"Content-Length":["247"],"Connection":["keep-alive"],"Server":["gunicorn\/19.9.0"],"Location":["\/relative-redirect\/1"],"Access-Control-Allow-Origin":["*"],"Access-Control-Allow-Credentials":["true"]}
                    [body] => <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/relative-redirect/1">/relative-redirect/1</a>.  If not click the link.
                    [contentLength] => 247
                    [reasonPhrase] => FOUND
                )

        )

    [1] => Array
        (
            [request] => Array
                (
                    [method] => GET
                    [url] => http://httpbin.org/relative-redirect/1
                    [headers] => {"Host":["httpbin.org"],"User-Agent":["GuzzleHttp\/7"]}
                    [body] => 
                    [protocol] => 1.1
                    [target] => /relative-redirect/1
                )

            [response] => Array
                (
                    [statusCode] => 302
                    [headers] => {"Date":["Mon, 13 Jan 2025 19:03:28 GMT"],"Content-Type":["text\/html; charset=utf-8"],"Content-Length":["0"],"Connection":["keep-alive"],"Server":["gunicorn\/19.9.0"],"Location":["\/get"],"Access-Control-Allow-Origin":["*"],"Access-Control-Allow-Credentials":["true"]}
                    [body] => 
                    [contentLength] => 0
                    [reasonPhrase] => FOUND
                )

        )

    [2] => Array
        (
            [request] => Array
                (
                    [method] => GET
                    [url] => http://httpbin.org/get
                    [headers] => {"Host":["httpbin.org"],"User-Agent":["GuzzleHttp\/7"]}
                    [body] => 
                    [protocol] => 1.1
                    [target] => /get
                )

            [response] => Array
                (
                    [statusCode] => 200
                    [headers] => {"Date":["Mon, 13 Jan 2025 19:03:30 GMT"],"Content-Type":["application\/json"],"Content-Length":["233"],"Connection":["keep-alive"],"Server":["gunicorn\/19.9.0"],"Access-Control-Allow-Origin":["*"],"Access-Control-Allow-Credentials":["true"]}
                    [body] => {
  "args": {}, 
  "headers": {
    "Host": "httpbin.org", 
    "User-Agent": "GuzzleHttp/7", 
    "X-Amzn-Trace-Id": "Root=1-67856380-2722bf0639f803eb79d6fd4b"
  }, 
  "origin": "IP REMOVED", 
  "url": "http://httpbin.org/get"
}

                    [contentLength] => 233
                    [reasonPhrase] => OK
                )

        )

)

print_r($client->getLastTransaction());

print_r($client->getTransactionSummary());

print_r($client->getDebug());

$client = new MiddlewareClient();
$response = $client->makeRequest('POST', 'http://localhost:8000/api/post', [
    'form_params' => ['username' => 'testuser', 'password' => 'secret123']
]);
print_r($client->getLastTransaction());


use FOfX\GuzzleMiddleware;
use FOfX\GuzzleMiddleware\MiddlewareClient;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Level;

// Create Monolog logger with stdout handler
$logger = new Logger('guzzle-middleware');
$logger->pushHandler(new StreamHandler('php://stdout', Level::Info));

// Create a new MiddlewareClient instance with logger
$client = new MiddlewareClient([], $logger);

$config = [];
$options = [];
$rotateUserAgent = false;

// Make a request to local dev server
$result = GuzzleMiddleware\makeMiddlewareRequest('GET', 'http://httpbin.org/redirect/2', $config, $options, $logger, $rotateUserAgent);
print_r($result);

$proxyConfig = ['proxy' => 'http://proxy.example.com:8000'];
$client = new MiddlewareClient(proxyConfig: $proxyConfig);
bash
php -S localhost:8000 public/dev-server.php