PHP code example of php-comp / http-client

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

    

php-comp / http-client example snippets


use PhpPkg\Http\Client\Client;

// use factory
$client = Client::factory([
    'driver' => 'curl', // stream, fsock, fopen, file, co, co2
    
    // ... 更多选项
    'baseUrl' =>  'http://my-site.com'
]);

$options = [
  'baseUrl' =>  'http://my-site.com'
  // ...
];
$client = CurlClient::create($options);
$client = FileClient::create($options);
$client = FSockClient::create($options);
$client = FOpenClient::create($options);
$client = CoClient::create($options);

use PhpPkg\Http\Client\Client;

$client = Client::factory([]);

$client->get('/users/1');

$post = ['name' => 'john'];
$client->post('/users/1', $post);

// add ajax header
$client->byAjax()->post('/users/1', $post);

// add json content type
$client->json('/users/1', json_encode($post));
// or
$client->byJson()->post('/users/1', json_encode($post));

$statusCode = $client->getStatusCode();
$headers = $client->getResponseHeaders();

// get body data
$data = $client->getResponseBody();
$array = $client->getArrayData();

$data = $client->getDataObject();
$data->getInt('createTime', 0);

$user = new User();
$client->bindBodyTo($user);
vdump($user->name);

$client = CurlClient::create([
  // ...
]);

$client->upload(...);