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);
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();
}