PHP code example of asika / http

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

    

asika / http example snippets

 php
use Asika\Http\HttpClient;

$http = new HttpClient;

$response = $http->get('http://example.com/?foo=bar');

// This is PSR7 ResponseInterface
(string) $response->getBody();
 php
$http = new HttpClient;

// The post data can be query string or array
$response = $http->post('http://example.com/?foo=bar', array('post_data' => 'data'));
$response = $http->put('http://example.com/?foo=bar', array('post_data' => 'data'));
$response = $http->patch('http://example.com/?foo=bar', array('post_data' => 'data'));
$response = $http->delete('http://example.com/?foo=bar', array('post_data' => 'data'));

$response = $http->head('http://example.com/?foo=bar');
$response = $http->trace('http://example.com/?foo=bar');
$response = $http->options('http://example.com/?foo=bar');

// With headers
$response = $http->get('http://example.com/', null, array('X-Foo' => 'Bar'));

// Use request()
$response = $http->request('POST', 'http://example.com/?foo=bar', 'this=is&post=data');
 php
use Asika\Http\Transport\CurlTransport;

$options = array(
    'certpath' => '/custom/cert.pem'
);

$transport = new CurlTransport($options);

// Set transport when client new
$http = new HttpClient(array(), $transport);
 php
$options = array(
    'options' => array(
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => true
    )
);

$httpOptions = array(
    'headers' => array(
        'X-Foo' => 'Bar'
    )
);

$http = new HttpClient($httpOptions, new CurlTransport($options));
 php
$http = new HttpClient;

$dest = '/path/to/local/file.zip';

$response = $http->download('http://example.com/file.zip', $dest);

if ($response->getStatusCode() != 200)
{
    // Error
}
 php
// new uri object
$uri = new Asika\Http\Uri\Uri;

$uri->setHost('http://localhost');
$uri->setPort('8888');
$uri->setUser('myUser');
$uri->setPass('myPass');

echo $uri->__toString();
 php
// set path
$uri->setPath('path/to/file.php');
 php
// url query
$uri->setQuery('foo=bar');

myUser:myPass@http://localhost:8888path/to/file.php?foo=bar
 php
$stream = new Stream('php://memory', 'wb+');

$stream->write('Foo Bar');

$stream->rewind(); // Back to begin

// Now we take something we wrote into memory

$stream->__toString(); // get: Foo Bar

// OR

$stream->rewind();
$stream->getContents(); // get: Foo Bar
 php
$stream = new PhpInputSteam;

$data = $stream->__toString(); // foo=bar

$query = \Asika\Http\Uri\UriHelper::parseQuery($data); // array('foo' => 'bar')
 php
$files = array();

foreach ($_FILE as $name => $file)
{
    $files[$name] = new UploadedFile($file['tmp_name'], $file['size'], $file['error'], $file['name'], $file['type']);
}

$request = new ServerRequest(
  $_SERVER,
  $_GET,
  $_POST,
  $_COOKIE,
  $files
);