PHP code example of piramit / http

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

    

piramit / http example snippets



use Piramit\Request;

$client = new Request();
$response = $client->get('https://api.example.com/data');

echo $response->getBody();


use Piramit\Request;

$client = new Request();
$response = $client->post('https://api.example.com/submit', [
    'json' => [
        'name' => 'John Doe',
        'email' => '[email protected]',
    ]
]);

echo $response->getStatusCode();  // 200
echo $response->getBody();        // Response body


$response = $client->get('https://api.example.com/data');
if ($response->getStatusCode() == 200) {
    echo 'Request was successful!';
} else {
    echo 'Request failed with status: ' . $response->getStatusCode();
}


$response = $client->get('https://api.example.com/data');
$headers = $response->getHeaders();
print_r($headers);


use Piramit\Exception\RequestException;

try {
    $response = $client->get('https://api.example.com/data');
} catch (RequestException $e) {
    echo 'Request failed: ' . $e->getMessage();
}


use Piramit\Request;

$client = new Request();

// Send GET request
$response = $client->get('https://api.example.com/data');

// Check if request was successful
if ($response->getStatusCode() == 200) {
    $data = json_decode($response->getBody(), true);
    echo 'Data received: ';
    print_r($data);
} else {
    echo 'Request failed with status: ' . $response->getStatusCode();
}

// Send POST request with JSON data
$response = $client->post('https://api.example.com/submit', [
    'json' => [
        'name' => 'John Doe',
        'email' => '[email protected]',
    ]
]);

if ($response->getStatusCode() == 200) {
    echo 'Data successfully submitted.';
} else {
    echo 'Submission failed with status: ' . $response->getStatusCode();
}


$client = new Request();
$response = $client->get('https://api.example.com/data', [
    'headers' => [
        'Authorization' => 'Bearer YOUR_API_TOKEN',
    ]
]);

echo $response->getBody();