Git Product home page Git Product logo

embryo-client's Introduction

Embryo Client

Simple PSR-18 HTTP Client.

Requirements

  • PHP >= 7.1

Installation

Using Composer:

$ composer require davidecesarano/embryo-client

Usage

You may use the get, post, put, patch, and delete methods provided by the Http Client factory. These methods returns an instance of Psr\Http\Message\ResponseInterface.

use Embryo\Http\Client\ClientFactory;

$http = new ClientFactory;
$response = $http->get('https://example.com/');

When making POST, PUT, and PATCH requests you may send additional data to request with an array of data as their second argument.

$response = $http->post('https://example.com/users', [
    'name' => 'Davide',
    'surname' => 'Cesarano'
]);

Headers

Headers may be added to requests using the withHeaders method. This method accepts an array of key / value pairs:

$response = $http
    ->withHeaders([
        'X-First' => 'foo',
        'X-Second' => 'bar'
    ])
    ->post('https://example.com/users', [
        'name' => 'Davide',
        'surname' => 'Cesarano'
    ]);

Bearer Token

If you would like to quickly add a bearer token to the request's Authorization header, you may use the withToken method:

$response = $http
    ->withToken('token')
    ->post('https://example.com/users', [
        'name' => 'Davide',
        'surname' => 'Cesarano'
    ]);

Sending a raw request body

You may use the withJson method if you want to provide a raw request body when making a request:

$response = $http
    ->withJson()
    ->post('https://example.com/users', [
        'name' => 'Davide',
        'surname' => 'Cesarano'
    ]);

Attach file

If you want to send files, you may use the withFile method. This method accepts the name of the field and file absolute path:

$response = $http
    ->withFile('file1', '/path/to/file1')
    ->withFile('file2', '/path/to/file2')
    ->post('https://example.com/upload');

Timeout

With timeout method you may to specify the maximum number of seconds to wait for a response:

$response = $http
    ->withTimeout(30)
    ->get('https://example.com/users');

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.