PHP code example of pdeans / http

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

    

pdeans / http example snippets


use pdeans\Http\Client;

$client = new Client();

// With options
$client = new Client([
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_SSL_VERIFYHOST => true,
]);

// GET request with header
$response = $client->get('https://example.com/1', ['custom-header' => 'header/value']);

// GET request without header
$response = $client->get('https://example.com/2');

// HEAD request
$response = $client->head('https://example.com/2');

// TRACE request
$response = $client->trace('https://example.com/2');

$headers = [
    'Content-Type'  => 'application/json',
    'Accept'        => 'application/json',
    'Authorization' => 'Basic ' . base64_encode('username:password'),
];

$data = json_encode(['json' => 'json data']);

// POST request with headers and request body
$response = $client->post('https://example.com/4', $headers, $data);

// PUT request
$response = $client->put('https://example.com/4', $headers, $data);

// PATCH request
$response = $client->patch('https://example.com/4', $headers, $data);

// OPTIONS request
$response = $client->options('https://example.com/4', $headers, $data);

// DELETE request
$response = $client->delete('https://example.com/4', $headers, $data);

use pdeans\Http\Factories\RequestFactory;

$request = (new RequestFactory())->createRequest('GET', 'https://example.com/1');

$response = $client->sendRequest($request);

use pdeans\Http\Request;

$request = new Request(
    uri: 'https://example.com',
    method: 'POST',
    headers: ['Content-Type' => 'application/json'],
    body: $client->getStream(json_encode(['json' => 'json data']))
);

$response = $client->sendRequest($request);

// Issue request
$response = $client->get('https://example.com/1', ['custom-header' => 'header/value']);

// Response body output
echo (string) $response->getBody();

// Response headers output
var_dump($response->getHeaders());
var_dump($response->getHeader('custom-header'));
var_dump($response->hasHeader('custom-header'));
echo $response->getHeaderLine('custom-header');

// Response status code output
echo $response->getStatusCode();

// Response reason phrase output
echo $response->getReasonPhrase();

use pdeans\Http\Factories\RequestFactory;
use pdeans\Http\Factories\ResponseFactory;
use pdeans\Http\Factories\ServerRequestFactory;
use pdeans\Http\Factories\StreamFactory;
use pdeans\Http\Factories\UploadedFileFactory;
use pdeans\Http\Factories\UriFactory;

// Psr\Http\Message\RequestFactoryInterface
$requestFactory = new RequestFactory();

// Psr\Http\Message\RequestInterface
$request = $requestFactory->createRequest('GET', 'https://example.com/1');

// Psr\Http\Message\ResponseFactoryInterface
$responseFactory = new ResponseFactory();

// Psr\Http\Message\ResponseInterface
$response = $responseFactory->createResponse();

// Psr\Http\Message\ServerRequestFactoryInterface
$serverRequestFactory = new ServerRequestFactory();

// Psr\Http\Message\ServerRequestInterface
$serverRequest = $serverRequestFactory->createServerRequest('GET', 'https://example.com/2');

// Psr\Http\Message\StreamFactoryInterface
$streamFactory = new StreamFactory();

// Psr\Http\Message\StreamInterface
$stream = $streamFactory->createStream();
$fileStream = $streamFactory->createStreamFromFile('dir/api.json');
$resourceStream = $streamFactory->createStreamFromResource(fopen('php://temp', 'r+'));

// Psr\Http\Message\UploadedFileFactoryInterface
$uploadedFileFactory = new UploadedFileFactory();

// Psr\Http\Message\UploadedFileInterface
$uploadedFile = $uploadedFileFactory->createUploadedFile($fileStream);

// Psr\Http\Message\UriFactoryInterface
$uriFactory = new UriFactory();

// Psr\Http\Message\UriInterface
$uri = $uriFactory->createUri();