PHP code example of solidworx / simple-http

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

    

solidworx / simple-http example snippets


use SolidWorx\SimpleHttp\HttpClient;

// Create a new client
$client = HttpClient::create();

// Create a client with a base URL
$client = HttpClient::createForBaseUrl('https://api.example.com');

use SolidWorx\SimpleHttp\HttpClient;

$response = HttpClient::create()
    ->url('https://api.example.com/users')
    ->get()
    ->request();

// Get the response content as a string
$content = $response->getContent();

// Get the response status code
$statusCode = $response->getStatusCode();

// Get the response headers
$headers = $response->getHeaders();

use SolidWorx\SimpleHttp\HttpClient;

$response = HttpClient::create()
    ->url('https://api.example.com/users')
    ->post()
    ->json([
        'name' => 'John Doe',
        'email' => '[email protected]'
    ])
    ->request();

// Parse the JSON response into an array
$data = $response->toArray();

// GET request
$client->get()->request();

// POST request
$client->post()->request();

// PUT request
$client->put()->request();

// PATCH request
$client->patch()->request();

// DELETE request
$client->delete()->request();

// OPTIONS request
$client->options()->request();

// Custom method
$client->method('CUSTOM')->request();

$client = HttpClient::create()
    ->url('https://api.example.com')
    ->header('Accept', 'application/json')
    ->header('X-API-Key', 'your-api-key');

$client = HttpClient::create()
    ->url('https://api.example.com/search')
    ->query([
        'q' => 'search term',
        'page' => 1,
        'limit' => 10
    ]);

$client = HttpClient::create()
    ->url('https://api.example.com/form')
    ->post()
    ->formData([
        'name' => 'John Doe',
        'email' => '[email protected]'
    ]);

$client = HttpClient::create()
    ->url('https://api.example.com/users')
    ->post()
    ->json([
        'name' => 'John Doe',
        'email' => '[email protected]'
    ]);

$client = HttpClient::create()
    ->url('https://api.example.com/data')
    ->post()
    ->body('Raw request body content');

$client = HttpClient::create()
    ->url('https://api.example.com/upload')
    ->post()
    ->uploadFile('file', '/path/to/file.pdf');

$client = HttpClient::create()
    ->url('https://api.example.com/protected')
    ->basicAuth('username', 'password');

$client = HttpClient::create()
    ->url('https://api.example.com/protected')
    ->bearerToken('your-token');

$client = HttpClient::create()
    ->url('https://api.example.com')
    ->disableSslVerification();

// Use HTTP/1.1 (default)
$client = HttpClient::create()
    ->url('https://api.example.com')
    ->httpVersion(HttpClient::HTTP_VERSION_1);

// Use HTTP/2
$client = HttpClient::create()
    ->url('https://api.example.com')
    ->http2();

$response = HttpClient::create()
    ->url('https://api.example.com/users')
    ->get()
    ->request();

// Get status code
$statusCode = $response->getStatusCode();

// Get headers
$headers = $response->getHeaders();
$contentType = $response->getHeaderLine('Content-Type');

// Get body as string
$content = $response->getContent();

// Parse JSON response
$data = $response->toArray();

// Save to a file
$response = HttpClient::create()
    ->url('https://example.com/large-file.zip')
    ->get()
    ->saveToFile('/path/to/save/file.zip')
    ->request();

// Append to a file
$response = HttpClient::create()
    ->url('https://example.com/data.txt')
    ->get()
    ->appendToFile('/path/to/existing/file.txt')
    ->request();

// Save response to a file after receiving it
$response = HttpClient::create()
    ->url('https://example.com/image.jpg')
    ->get()
    ->request();

$response->saveToFile('/path/to/save/image.jpg');

$response = HttpClient::create()
    ->url('https://example.com/large-file.zip')
    ->progress(function ($progress) {
        // $progress->getDownloaded() - bytes downloaded
        // $progress->getTotal() - total bytes (if known)
        echo "Downloaded {$progress->getDownloaded()} of {$progress->getTotal()} bytes\n";
    })
    ->request();

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter();

$response = HttpClient::create()
    ->url('https://api.example.com/data')
    ->cacheResponse($cache, 3600) // Cache for 1 hour
    ->request();

use SolidWorx\SimpleHttp\HttpClient;
use Http\Client\Exception\HttpException;

try {
    $response = HttpClient::create()
        ->url('https://api.example.com/users')
        ->get()
        ->request();
} catch (HttpException $e) {
    // Get the error response
    $errorResponse = $e->getResponse();

    // Get the status code
    $statusCode = $errorResponse->getStatusCode();

    // Get the error message
    $errorMessage = $e->getMessage();

    // Get the response body
    $errorBody = $errorResponse->getBody()->getContents();
}

$client = HttpClient::createForBaseUrl('https://api.example.com');

// Now you can make requests to endpoints relative to the base URL
$response = $client->path('/users')->get()->request();
$response = $client->path('/posts')->get()->request();

$client = HttpClient::create()->url('https://api.example.com');

// These create new instances, they don't modify the original $client
$jsonClient = $client->header('Accept', 'application/json');
$authClient = $client->bearerToken('your-token');

// Original client remains unchanged
$response = $client->request();

$baseClient = HttpClient::createForBaseUrl('https://api.example.com')
    ->bearerToken('your-token')
    ->header('Accept', 'application/json');

// Reuse the base client for different endpoints
$usersResponse = $baseClient->path('/users')->get()->request();
$postsResponse = $baseClient->path('/posts')->get()->request();
bash
composer