Giter VIP home page Giter VIP logo

http's Introduction

build status

Dependency Status

SensioLabsInsight

A smart, simple and fault-tolerant HTTP client for sending and recieving JSON and XML.

Installation

Composer

composer require vinelab/http

// change this to point correctly according
// to your folder structure.
require './vendor/autoload.php';

use Vinelab\Http\Client as HttpClient;

$client = new HttpClient;

$response = $client->get('echo.jsontest.com/key/value/something/here');

var_dump($response->json());

Laravel

Edit app.php and add 'Vinelab\Http\HttpServiceProvider', to the 'providers' array.

It will automatically alias itself as HttpClient so no need to alias it in your app.php, unless you would like to customize it - in that case edit your 'aliases' in app.php adding 'MyHttp' => 'Vinelab\Http\Facades\Client',

Usage

GET

Simple

$response = HttpClient::get('http://example.org');

// raw content
$response->content();

With Params

$request = [
	'url' => 'http://somehost.net/something',
	'params' => [

		'id'     => '12350ME1D',
		'lang'   => 'en-us',
		'format' => 'rss_200'
	]
];

$response = HttpClient::get($request);

// raw content
$response->content();

// in case of json
$response->json();

// XML
$response->xml();

POST

$request = [
	'url' => 'http://somehost.net/somewhere',
	'params' => [

		'id'     => '12350ME1D',
		'lang'   => 'en-us',
		'format' => 'rss_200'
	]
];

$response = HttpClient::post($request);

// raw content
$response->content();

// in case of json
$response->json();

// XML
$response->xml();

Options

These options work with all requests.

Timeout

You can set the timeout option in order to specify the number of seconds that your request will fail, if not completed.

$request = [
	'url' => 'http://somehost.net/somewhere',
	'params' => [

		'id'     => '12350ME1D',
		'lang'   => 'en-us',
		'format' => 'rss_200'
	],
	'timeout' => 10
];

Headers

$response = HttpClient::get([
	'url' => 'http://somehost.net/somewhere',
	'headers' => ['Connection: close', 'Authorization: some-secret-here']
]);

// The full headers payload
$response->headers();

Basic Auth

$response = HttpClient::get([
	'url' => 'http://somehost.net/somewhere',
	'auth' => [
		'username' => 'user',
		'password' => 'pass'
	],
	'params' => [
		'var1'     => 'value1',
		'var2'   => 'value2'
	]
]);

Digest Auth

$response = HttpClient::get([
	'url' => 'http://some.where.url',
	'digest' => [
		'username' => 'user',
		'password' => 'pass'
	],
	'params' => [
		'var1'     => 'value1',
		'var2'   => 'value2'
	]
]);

Enforce HTTP Version

HttpClient::get(['version' => 1.1, 'url' => 'http://some.url']);

Raw Content

HttpClient::post(['url' => 'http://to.send.to', 'content' => 'Whatever content here may go!']);

Custom Query String

The content passed in the content key will be concatenated to the URL followed by a ?

HttpClient::get(['url' => 'http://my.url', 'content' => 'a=b&c=d']);

It is pretty much the same process with different HTTP Verbs. Supports GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD

Fault Tolerance

Fault tolerance allows the request to be re-issued when it fails (i.e. timeout). This is useful in cases such as Microservices: When a service is down and is being called by another service, with fault tolerance the request will be re-issued in the hopes of the destination service being up again.

Issue a fault-tolerant request by setting the tolerant flag to true in the request. Also, specify the time it should wait until it tries again with timeUntilNextTry (in seconds) and the number of tries before it is considered a failure with triesUntilFailure (in seconds).

$request = [
	'url' => 'http://somehost.net/somewhere',
	'params' => [

		'id'     => '12350ME1D',
		'lang'   => 'en-us',
		'format' => 'rss_200'
	],
	'timeout' => 10
	'tolerant' => true,
	'timeUntilNextTry' => 1,
	'triesUntilFailure' => 3
];

In case of timeout occurance, a HttpClientRequestFailedException will be thrown.

IMPORTANT! Notice: In order to make use of fault tolerance option, you must specify the timeout parameter too.

http's People

Contributors

adibhanna avatar alisissa avatar amaelftah avatar gjrdiesel avatar harris21 avatar messi89 avatar mulkave avatar nickma42 avatar ybr-nx avatar znarkus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

http's Issues

Requirements could not be resolved to an installable set of packages.

Hi

I can't install the package on the latest laravel version
`Your requirements could not be resolved to an installable set of packages.

Problem 1
- vinelab/http v1.5.0 requires tolerance/tolerance ^0.3.3 -> satisfiable by tolerance/tolerance[0.3.3].
- vinelab/http v1.5.1 requires tolerance/tolerance ^0.3.3 -> satisfiable by tolerance/tolerance[0.3.3].
- vinelab/http v1.5.2 requires tolerance/tolerance ^0.3.3 -> satisfiable by tolerance/tolerance[0.3.3].
- Conclusion: don't install tolerance/tolerance 0.3.3
- Installation request for vinelab/http ^1.5 -> satisfiable by vinelab/http[v1.5.0, v1.5.1, v1.5.2].

Installation failed, reverting ./composer.json to its original content.
`

Laravel 5.3.*

Posting JSON

Posting JSON isn't really posted as JSON and tests are failing since the file /tests/server/json_post.php isn't capturing the JSON data, and cURL doesn't populate passed data to the superglobal $_POST so we need to send params json encoded.

Support Laravel 4.1

Is it possible to change the requirement in composer.json so that an update to Laravel 4.1 is possible?

Laravel 5

vinelab/http dev-master requires illuminate/support 4.*
Will there be an update?

Out dated dependencies

How do I get around the outdated dependency on this package?

I have a project done in laravel 4.2 and I want to update it to latest laravel version.

Thanks,

CookieContenair

Hi

is this package support hanling cookies from server ? (like CookieContenair or CookieJar)

Missing features

Hello @Mulkave

I dlike to create a http client adapter to my https://github.com/saxulum/saxulum-http-client-interface, cause its seems your client is very popular within the lavarel community.

There are allready 3:

There are two features i miss:

  • plain content support
  • protocol enforcement (1.0, 1.1)

Would you like to add them?

Regards @dominikzogg

Get cookie values

I have a api where there are duplicated header keys. Is it possible to extract the headers with ignoring parseHeaders?

I changed the function:

protected function parseHeaders($response, $headerSize)
{
    $headers = substr($response, 0, $headerSize);
    $parsedHeaders = [];

    foreach (explode("\r\n",$headers) as $header)
    {
        if (strpos($header, ':'))
        {
            $nestedHeader = explode(':', $header);
            $parsedHeaders[$nestedHeader[0]][] = trim($nestedHeader[1]);
        }
    }

    return $parsedHeaders;
}

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.