Coder Social home page Coder Social logo

shuttle's Introduction

Shuttle

Latest Stable Version GitHub Workflow Status Codecov branch License

A simple PSR-18 HTTP client library.

Installation

composer require nimbly/shuttle

Features

  • Responses create php://temp response body stream and swap to disk when necessary.
  • cURL (default) and Stream Context handlers supported.
  • Middleware support out of the box.
  • Easy body transformations when creating requests with JsonBody, FormBody, and XmlBody helper classes.

Not features

  • Asynchronous calls.

Making requests: The easy way

The quickest and easiest way to begin making requests in Shuttle is to use the HTTP method name:

use Nimbly\Shuttle\Shuttle;

$shuttle = new Shuttle;

$response = $shuttle->get("https://www.google.com");
$response = $shuttle->post("https://example.com/search", "Form data"));

Shuttle has built-in methods to support the major HTTP verbs: get, post, put, patch, delete, head, and options. However, you can make any HTTP verb request using the request method directly.

$response = $shuttle->request("connect", "https://api.example.com/v1/books");

Handling responses

Responses in Shuttle implement PSR-7 ResponseInterface and as such are streamable resources.

$response = $shuttle->get("https://api.example.com/v1/books");

echo $response->getStatusCode(); // 200
echo $response->getReasonPhrase(); // OK
echo $response->isSuccessful(); // true

$body = $response->getBody()->getContents();

Handling failed requests

Shuttle will throw a RequestException by default if the request failed. This includes things like host name not found, connection timeouts, etc.

Responses with HTTP 4xx or 5xx status codes will not throw an exception and must be handled properly within your business logic.

Making requests: The PSR-7 way

If code reusability and portability is your thing, future proof your code by making requests the PSR-7 way. Remember, PSR-7 stipulates that Request and Response messages be immutable.

// Build Request message with your favorite PSR-7 library.
$request = new Request("get", "https://www.example.com");

// Send the Request.
$shuttle = new Shuttle;
$response = $shuttle->sendRequest($request);

Request bodies

An easy way to submit data with your request is to use the \Shuttle\Body\* helper classes. These classes will automatically transform the data, convert to a BufferStream, and set a default Content-Type header on the request.

The request bodies support are:

  • JsonBody Converts an associative array into JSON, sets Content-Type header to application/json.
  • FormBody Converts an associative array into a query string, sets Content-Type header to application/x-www-form-urlencoded.
  • XmlBody Does no conversion of data, sets Content-Type header to application/xml.

To submit a JSON payload with a request:

use Nimbly\Shuttle\Body\JsonBody;

$book = [
    "title" => "Breakfast Of Champions",
    "author" => "Kurt Vonnegut",
];

$shuttle->post("https://api.example.com/v1/books", new JsonBody($book));

shuttle's People

Contributors

8ctopus avatar brentscheffler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

8ctopus

shuttle's Issues

Disable `CURLOPT_SSL_VERIFYPEER`

Thank you for a nice http client which I have been using for already quite some time.

I'm currently trying to figure out how to disable CURLOPT_SSL_VERIFYPEER and it seems it is not possible as even if the $curl_options has it set to false, it will never override the original value (see array + operator https://www.php.net/manual/en/language.operators.array.php)

	protected array $options = [
		CURLOPT_SSL_VERIFYPEER => true,
	];

	public function __construct(
		array $curl_options = [])
	{
		$handle = \curl_init();

		if( $handle === false ){
			throw new HandlerException("Could not initialize cURL.");
		}

		$this->curlHandle = $handle;
		$this->options += $curl_options;
	}

I managed to get the expected behavior by modifying the options line like this:

		$this->options = $curl_options + $this->options;

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.