https://github.com/moccalotto/hayttp
HTTP request made easy!
https://github.com/moccalotto/hayttp
api-client decode-responses http http-client json php xml
Last synced: 3 months ago
JSON representation
HTTP request made easy!
- Host: GitHub
- URL: https://github.com/moccalotto/hayttp
- Owner: moccalotto
- License: mit
- Created: 2016-07-26T14:32:14.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-01-24T07:17:23.000Z (about 7 years ago)
- Last Synced: 2025-08-13T06:21:58.375Z (8 months ago)
- Topics: api-client, decode-responses, http, http-client, json, php, xml
- Language: PHP
- Homepage: https://moccalotto.github.io/docs/hayttp
- Size: 229 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hayttp
[](https://travis-ci.org/moccalotto/hayttp)
HTTP request made easy!
* Lightweight, fast and small footprint.
* Syntacticly sweet, easy and intuitive.
* Short-hands to the 7 RESTful HTTP methods.
* Real file (and blob) uploads.
* Basic Auth.
* Immutable.
* Awesome advanced options:
* Choose between CURL and php native http streams.
* Create your own http transport engine (for instance a guzzle wrapper).
* Choose ssl/tls scheme and version.
* Create custom payloads.
## Installation
To add this package as a local, per-project dependency to your project, simply add a dependency on
`moccalotto/hayttp` to your project's `composer.json` file like so:
```json
{
"require": {
"moccalotto/hayttp": "~1.0"
}
}
```
Alternatively execute the following command in your shell.
```bash
composer require moccalotto/hayttp
```
## Usage
```php
use Hayttp\Hayttp;
$response = Hayttp::get($url)->send();
```
### REST Methods
Hayttp is essentially a factory that can create and initialize `Request` objects.
It has methods for each of the 7 RESTful HTTP methods.
Making GET Requests:
```php
$response = Hayttp::get($url)->send();
```
A more interesting POST example.
```php
$response = Hayttp::post($url)
->acceptJson()
->sendJson([
'this' => 'associative',
'array' => 'will',
'be' => 'converted',
'to' => 'a',
'json' => 'object',
]);
```
A DELETE request that accept an XML body in the response.
```php
$response = Hayttp::delete($url)
->acceptXml()
->send();
```
### Decode responses
You can parse/unserialize the response payloads into native php data structures.
Hayttp currently supports json, xml and rfc3986.
Below is an example of how parse a response as json.
Json objects are converted to `stdClass` objects, and json arrays are converted to php arrays:
```php
$stdClass = Hayttp::get($url)
->acceptJson()
->send()
->jsonDecoded();
```
Here is an example of a response decoded into a `SimpleXmlElement`:
```php
$simpleXmlElement = Hayttp::get($url)
->acceptXml()
->send()
->xmlDecoded();
```
Decode a url-encoded string into an associative array:
```php
$array = Hayttp::get($url)
->send()
->urlDecoded();
```
Decode the respose, inferring the data type from the Content-Type header:
```php
$variable = Hayttp::get($url)->send()->decoded();
```
### Helper function
You can use the global `hayttp` method to access the default hayttp instance.
```php
$body = hayttp()->withTimeout(10)
->post($url)
->ensureJson()
->sendJson(['foo' => 'bar',])
->decded();
```
You can also use the `hayttp_*` method to make instant requests.
```php
// All the calls below are equivalent
$response = hayttp_get($url);
$response = Hayttp::get($url)
->ensure2xx()
->send();
$response = hayttp()->get($url)
->ensure2xx()
->send();
```
Here are other examples of how to use the `hayttp_*` methods:
```php
// All the calls below are equivalent
$xml = new SimpleXmlElement('Boot');
$response = hayttp_post($url, $xml);
$response = Hayttp::post($url)
->ensure2xx()
->sendXml($xml);
$response = hayttp()->post($url)
->ensure2xx()
->sendXml($xml);
```
Posting json
```php
// All the calls below are equivalent
$data = ['foo' => ['bar' => ['baz']]];
$response = hayttp_post($url, $data);
$response = Hayttp::post($url)
->ensure2xx()
->sendJson($data);
$response = hayttp()->post($url)
->ensure2xx()
->sendJson($data);
```
Putting raw text
```php
// All the calls below are equivalent
$raw = file_get_contents($path);
$response = hayttp_put($url, $raw);
$response = Hayttp::put($url)
->ensure2xx()
->sendRaw($raw, 'application/octet-stream');
$response = hayttp()->put($url)
->ensure2xx()
->sendRaw($raw, 'application/octet-stream');
```