PHP code example of vaibhavpandeyvpz / dakiya

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

    

vaibhavpandeyvpz / dakiya example snippets




use Dakiya\Client;
use Sandesh\RequestFactory;
use Sandesh\ResponseFactory;

// Create the client with a response factory
$client = new Client(new ResponseFactory());

// Create a request
$requestFactory = new RequestFactory();
$request = $requestFactory->createRequest('GET', 'https://api.example.com/users');

// Send the request
$response = $client->sendRequest($request);

// Check the response
if ($response->getStatusCode() === 200) {
    $body = (string) $response->getBody();
    $data = json_decode($body, true);
    // Process the data...
}

use Dakiya\Client;
use Sandesh\RequestFactory;
use Sandesh\ResponseFactory;

$client = new Client(new ResponseFactory());
$requestFactory = new RequestFactory();

$request = $requestFactory->createRequest('GET', 'https://api.example.com/data');
$response = $client->sendRequest($request);

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

use Dakiya\Client;
use Sandesh\RequestFactory;
use Sandesh\ResponseFactory;
use Sandesh\StreamFactory;

$client = new Client(new ResponseFactory());
$requestFactory = new RequestFactory();
$streamFactory = new StreamFactory();

$body = json_encode(['name' => 'John Doe', 'email' => '[email protected]']);
$request = $requestFactory->createRequest('POST', 'https://api.example.com/users')
    ->withBody($streamFactory->createStream($body))
    ->withHeader('Content-Type', 'application/json');

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

$data = http_build_query(['username' => 'johndoe', 'password' => 'secret']);
$request = $requestFactory->createRequest('POST', 'https://api.example.com/login')
    ->withBody($streamFactory->createStream($data))
    ->withHeader('Content-Type', 'application/x-www-form-urlencoded');

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

$uri = $requestFactory->createUri('https://api.example.com/protected')
    ->withUserInfo('username', 'password');

$request = $requestFactory->createRequest('GET', $uri);
$response = $client->sendRequest($request);

$client = new Client(new ResponseFactory(), [
    CURLOPT_TIMEOUT => 30,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_USERAGENT => 'MyApp/1.0',
    CURLOPT_SSL_VERIFYPEER => true,
]);

$request = $requestFactory->createRequest('GET', 'https://api.example.com/data');
$response = $client->sendRequest($request);

// HTTP/1.0
$request = $requestFactory->createRequest('GET', 'https://api.example.com/data')
    ->withProtocolVersion('1.0');

// HTTP/1.1 (default)
$request = $requestFactory->createRequest('GET', 'https://api.example.com/data')
    ->withProtocolVersion('1.1');

// HTTP/2.0
$request = $requestFactory->createRequest('GET', 'https://api.example.com/data')
    ->withProtocolVersion('2.0');

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

use Dakiya\NetworkException;

try {
    $response = $client->sendRequest($request);
} catch (NetworkException $e) {
    // Network-level errors (connection failures, timeouts, etc.)
    echo "Network error: " . $e->getMessage();
    $failedRequest = $e->getRequest();
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
    // Other client exceptions
    echo "Client error: " . $e->getMessage();
}

$largeFile = file_get_contents('large-file.bin');
$request = $requestFactory->createRequest('POST', 'https://api.example.com/upload')
    ->withBody($streamFactory->createStream($largeFile))
    ->withHeader('Content-Type', 'application/octet-stream');

// Automatically uses streaming for bodies > 1MB
$response = $client->sendRequest($request);

public function __construct(
    ResponseFactoryInterface $factory,
    array $options = []
)