PHP code example of devuri / http

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

    

devuri / http example snippets




risoft\HttpClient;

// Initialize HttpClient with the base URL of the API
$client = new HttpClient('https://api.example.com');

// Perform a GET request
$response = $client->get('/data');
print_r($response);

// Perform a POST request
$postData = ['key' => 'value'];
$response = $client->post('/submit', $postData);
print_r($response);

$client->set_user_agent('Mozilla/5.0 (compatible; CustomBot/1.0)');

$client->set_referrer('https://referrer.example.com');

$options = [
    'user_agent' => 'safari',
    'api_key' => 'your_api_key_here',
    'timeout' => 30, // in seconds
];
$client = new HttpClient('https://api.example.com', $options);



risoft\HttpClient;

// Initialize the client with the base URL
$client = new HttpClient('https://jsonplaceholder.typicode.com');

// Fetch posts from an API
$response = $client->get('/posts');
print_r($response);

$client = new HttpClient('https://jsonplaceholder.typicode.com');

// Data to be sent
$data = ['title' => 'foo', 'body' => 'bar', 'userId' => 1];

// Send data
$response = $client->post('/posts', $data);
print_r($response);

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

// Custom headers
$headers = [
    'Content-Type: application/json',
    'Custom-Header: value'
];

// Perform a GET request with custom headers
$response = $client->get('/endpoint', $headers);
print_r($response);

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

try {
    $response = $client->get('/data');
    if ($response['status'] !== 200) {
        throw new Exception('Failed to fetch data: ' . $response['message']);
    }
    echo "Data fetched successfully:\n";
    print_r($response);
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

$options = [
    'user_agent' => 'safari',
    'api_key' => 'your_secure_api_key',
    'timeout' => 20
];

$client = new HttpClient('https://api.example.com', $options);
$client->set_referrer('https://referrer.example.com');
$response = $client->post('/submit', ['data' => 'value']);
print_r($response);

if ($response['status'] !== 200) {
    echo "Error: " . $response['message'];
} else {
    echo "Success: " . $response['message'];
}

$response = $client->get('/api/data');

print_r($response);