1. Go to this page and download the library: Download callismart/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/ */
callismart / http example snippets
use Callismart\Http\HttpClient;
$client = new HttpClient();
$response = $client->get( 'https://api.example.com/v1/resource' );
if ( $response->ok() ) {
$data = $response->json();
print_r( $data );
}
$client = new HttpClient();
// Automatically uses the first available adapter
use Callismart\Http\Adapters\CurlAdapter;
use Callismart\Http\Adapters\FopenAdapter;
use Callismart\Http\Adapters\SocketAdapter;
// Force cURL
$client = new HttpClient( new CurlAdapter() );
// Force fopen
$client = new HttpClient( new FopenAdapter() );
// Force socket (always available)
$client = new HttpClient( new SocketAdapter() );
$client->with_default_header( 'Authorization', "Bearer {$token}" )
->with_default_header( 'User-Agent', 'MyApp/1.0' );
// These headers are merged into every request sent via this client
$response = $client->get( 'https://api.example.com/user' );
// Automatically
use Callismart\Http\HttpRequest;
// GET
$request = HttpRequest::get( 'https://api.example.com/posts' );
// POST
$request = HttpRequest::post( 'https://api.example.com/posts', $body );
// PUT
$request = HttpRequest::put( 'https://api.example.com/posts/123', $body );
// PATCH
$request = HttpRequest::patch( 'https://api.example.com/posts/123', $body );
// DELETE
$request = HttpRequest::delete( 'https://api.example.com/posts/123' );
// GET
$response = $client->get( 'https://api.example.com/users' );
// POST with body
$response = $client->post( 'https://api.example.com/users', $json_body );
// POST with JSON (automatic encoding + headers)
$response = $client->post_json( 'https://api.example.com/users', [
'name' => 'Bob',
] );
// PUT
$response = $client->put( 'https://api.example.com/users/123', $body );
// PATCH
$response = $client->patch( 'https://api.example.com/users/123', $body );
// DELETE
$response = $client->delete( 'https://api.example.com/users/123' );
// Download (with automatic streaming)
$response = $client->download( $url, '/path/to/file' );
$response = $client->get( $url );
// Shorthand for is_success()
if ( $response->ok() ) {
// 2xx status
}
// Detailed checks
if ( $response->is_success() ) {
// 200-299
}
if ( $response->is_client_error() ) {
// 400-499
}
if ( $response->is_server_error() ) {
// 500-599
}
if ( $response->is_error() ) {
// 400-599
}
if ( $response->is_redirect() ) {
// 300-399
}
// JSON decoding
$data = $response->json(); // returns decoded array (or null if invalid)
// Check if body is valid JSON
if ( $response->is_json() ) {
$data = $response->json();
}
// Raw body as string
$raw = $response->body;
// Get a single header (case-insensitive)
$content_type = $response->get_header( 'content-type' );
// Check if header exists
if ( $response->has_header( 'content-length' ) ) {
// ...
}
// Shorthand for Content-Type
$type = $response->content_type(); // e.g. "application/json; charset=utf-8"
// All headers as associative array
$headers = $response->headers; // keys are lowercase
// Get a single cookie
$session_id = $response->get_cookie( 'session_id' );
// Check if cookie exists
if ( $response->has_cookie( 'session_id' ) ) {
// ...
}
// All cookies as associative array
$cookies = $response->cookies;
// Check if any redirects were followed
if ( $response->was_redirected() ) {
echo "Original URL: {$response->original_url()}";
echo "Final URL: {$response->final_url()}";
}
// Complete redirect history (ordered list of URLs)
foreach ( $response->redirect_history as $url ) {
echo "Followed: {$url}\n";
}
$response = $client->download( $url, '/tmp/file.zip' );
// Check if response was streamed to file
if ( $response->is_download() ) {
echo "File: {$response->sink_path}";
echo "Size: {$response->file_size} bytes";
echo "Body is empty: " . ( $response->body === '' ? 'yes' : 'no' );
}
$response = $client->get( $url ); // buffered in memory
if ( $response->is_success() ) {
$response->save_to( '/tmp/file.zip' ); // write to disk
}
$response = $client->get( $url );
echo "Request took {$response->latency} seconds";
use Callismart\Http\AwsSignatureV4;
$signer = new AwsSignatureV4(
access_key: 'AKIAIOSFODNN7EXAMPLE',
secret_key: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
region: 'us-east-1',
service: 'ses', // or 's3', 'lambda', etc.
);
use Callismart\Http\Adapters\CurlAdapter;
$adapter = new CurlAdapter();
if ( $adapter->is_available() ) {
echo "cURL is available";
}
use Callismart\Http\Adapters\FopenAdapter;
$adapter = new FopenAdapter();
if ( $adapter->is_available() ) {
echo "fopen streaming is available";
}
use Callismart\Http\Adapters\SocketAdapter;
$adapter = new SocketAdapter();
if ( $adapter->is_available() ) {
echo "Socket fallback is available";
}
$client = new HttpClient();
// Automatically selects cURL → fopen → socket
// If none are available, throws HttpRequestException:
// "HttpClient: no HTTP adapter is available in this environment.
// Enable cURL, allow_url_fopen, or fsockopen."
$client = new HttpClient( new SocketAdapter() );
// Uses socket even if cURL is available