Download the PHP package chh/httpfetch without Composer
On this page you can find all versions of the php package chh/httpfetch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chh/httpfetch
More information about chh/httpfetch
Files in chh/httpfetch
Package httpfetch
Short Description A library for simple HTTP requests (using Guzzle Ring)
License MIT
Homepage https://github.com/CHH/httpfetch.git
Informations about the package httpfetch
httpfetch
httpfetch provides a simple function fetch
to make HTTP requests in small scripts easy and fast.
httpfetch relies heavily on RingPHP for its robust low level HTTP abstraction and for the ability to make asynchronous requests.
Use Cases
- As replacement for
file_get_contents
, but with async support, proper support for all HTTP features, easy passing of request headers, parsing of response headers, and proper handling of the certificates used by HTTPS. file_get_contents
is notoriously insecure when used with HTTPS without proper configuration, which is hard to do. Also see this secure file_get_contents wrapper.- Making simple web service clients without depending on Guzzle.
Install
Via Composer
Usage
The chh\httpfetch Namespace
fetch($url, array $options = [])
Makes a HTTP request to the provided URL. The options follow the RingPHP specification for requests and the returned response follows the RingPHP specification for responses.
All requests are made asynchronously by default, when supported by the handler. This can be turned off by setting the future
option to false
.
httpfetch implements a few additional options for convenience:
follow_location
(default:true
): Follows responses which return a "Location" headermax_redirects
(default:10
): Number of redirects to followauth
(default:null
): Username/password pair as array for Basic Authentication, e.g.["user", "password"]
Responses are array-like objects with the following keys:
body
: (string, fopen resource, Iterator, GuzzleHttp\Stream\StreamInterface) The body of the response, if present. Can be a string, resource returned from fopen, an Iterator that yields chunks of data, an object that implemented __toString, or a GuzzleHttp\Stream\StreamInterface.effective_url
: (string) The URL that returned the resulting response.error
: (\Exception) Contains an exception describing any errors that were encountered during the transfer.headers
: (Required, array) Associative array of headers. Each key represents the header name. Each value contains an array of strings where each entry of the array is a header line. The headers array MAY be an empty array in the event an error occurred before a response was received.reason
(string) Optional reason phrase. This option should be provided when the reason phrase does not match the typical reason phrase associated with the status code. See RFC 7231 for a list of HTTP reason phrases mapped to status codes.status
: (Required, integer) The HTTP status code. The status code MAY be set to null in the event an error occurred before a response was received (e.g., a networking error).transfer_stats
: (array) Provides an associative array of arbitrary transfer statistics if provided by the underlying handler.version
: (string) HTTP protocol version. Defaults to 1.1.
For example a POST request by using the http_method
parameter:
Example: Doing an async GET request with the Promise API:
Example: Doing an async request with the Future API:
Example: Doing requests in parallel:
get(), post(), put(), delete(), head(), options()
Helper methods for common HTTP methods. They all follow the same signature of ($url, array $options = [])
.
Example:
set_default_handler(callable $handler)
Overrides the Guzzle Ring Client handler which is used by the fetch
function. Handlers are callables which follow the Ring specification. Reset the handler to the default by passing null
.
Example: Force the usage of PHP's http:// stream wrapper:
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Christoph Hochstrasser
- All Contributors
License
The MIT License (MIT). Please see License File for more information.