PHP code example of arhitector / http-curl-client

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

    

arhitector / http-curl-client example snippets


use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;

$client = new Mackey\Http\Client\Curl\Client(new DiactorosMessageFactory(), new DiactorosStreamFactory(), [
  // array, set default curl options, if you need
  CURLOPT_SSL_VERIFYPEER => false
]);

$request = new Zend\Diactoros\Request('<url>', 'GET');

$response = $client->sendRequest($request, [
  // curl options for 1 request, if you need
  CURLOPT_FOLLOWLOCATION => true
]);

$request = new Zend\Diactoros\Request('<url>', 'GET');

$promise = $client->sendAsyncRequest($request);
$promise->then(
  function (ResponseInterface $response) {
    // The success callback

    return $response;
  },
  function (\Exception $exception) {
    // The failure callback

    throw $exception;
  }
);

// other request
// $promise = $client->sendAsyncRequest($request);

try {
    $response = $promise->wait();
} catch (\Exception $exception) {
    echo $exception->getMessage();
}

// example just send file in output
$stream = new \Zend\Diactoros\Stream('file/to/send.txt');
$response = new \Zend\Diactoros\Response($stream, 200);

$client->sendResponse($response);

// or send for download
$response = $response->withHeader('Content-Description', 'File Transfer')
    ->withHeader('Content-Type', 'application/octet-stream')
    ->withHeader('Content-Disposition', 'attachment; filename=filename.txt')
    ->withHeader('Content-Transfer-Encoding', 'binary');

$client->sendResponse($response);