PHP code example of workerman / http-client

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

    

workerman / http-client example snippets



use Workerman\Worker;

rker->onWorkerStart = function () {
    $http = new Workerman\Http\Client();

    $http->get('https://example.com/', function ($response) {
        var_dump($response->getStatusCode());
        echo $response->getBody();
    }, function ($exception) {
        echo $exception;
    });

    $http->post('https://example.com/', ['key1' => 'value1', 'key2' => 'value2'], function ($response) {
        var_dump($response->getStatusCode());
        echo $response->getBody();
    }, function ($exception) {
        echo $exception;
    });

    $http->request('https://example.com/', [
        'method' => 'POST',
        'version' => '1.1',
        'headers' => ['Connection' => 'keep-alive'],
        'data' => ['key1' => 'value1', 'key2' => 'value2'],
        'success' => function ($response) {
            echo $response->getBody();
        },
        'error' => function ($exception) {
            echo $exception;
        }
    ]);
};
Worker::runAll();


orkerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function(){
    $options = [
        'max_conn_per_addr' => 128,
        'keepalive_timeout' => 15,
        'connect_timeout'   => 30,
        'timeout'           => 30,
    ];
    $http = new Workerman\Http\Client($options);

    $http->get('http://example.com/', function($response){
        var_dump($response->getStatusCode());
        echo $response->getBody();
    }, function($exception){
        echo $exception;
    });
};
Worker::runAll();


use Workerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function(){
    $options = [
        'max_conn_per_addr' => 128,
        'keepalive_timeout' => 15,
        'connect_timeout'   => 30,
        'timeout'           => 30,
        // 'context' => [
        //     'http' => [
        //         // All use '$http' will cross proxy.  The highest priority here. !!!
        //         'proxy' => 'http://127.0.0.1:1080',
        //     ],
        // ],
    ];
    $http = new Workerman\Http\Client($options);

    $http->request('https://example.com/', [
        'method' => 'GET',
        'proxy' => 'http://127.0.0.1:1080',
         // 'proxy' => 'socks5://127.0.0.1:1081',
        'success' => function ($response) {
            echo $response->getBody();
        },
        'error' => function ($exception) {
            echo $exception;
        }
    ]);
};
Worker::runAll();


use Workerman\Worker;

);
$worker->onWorkerStart = function () {
    $http = new Workerman\Http\ParallelClient();

    $http->batch([
        ['http://example.com', ['method' => 'POST', 'data' => ['key1' => 'value1', 'key2' => 'value2']]],
        ['https://example2.com', ['method' => 'GET']],
    ]);
    $http->push('http://example3.com');

    $result = $http->await(false);
    // $result:
    // [
    //     [bool $isSuccess = true, Workerman\Http\Response $response],
    //     [bool $isSuccess = false, Throwable $error],
    //     [bool $isSuccess, Workerman\Http\Response $response],
    // ]

};
Worker::runAll();