sfadless / http
Small library for get/post requests
Installs: 47
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/sfadless/http
Requires
- php: >=5.4
README
Documentation
Instalation
composer require Sfadless/http
Usage
Basic usage
use Sfadless\Utils\Http\Http; require_once __DIR__ . '/vendor/autoload.php'; $http = new Http(); $response = $http->get('http://some.url'); $response = $http->post('http://some.url');
Usage with params and headers
use Sfadless\Utils\Http\Http; require_once __DIR__ . '/vendor/autoload.php'; $http = new Http(); $params = [ 'param1' => 'value', 'param2' => 'value2' ]; $headers = [ 'header1' => 'value', 'header2' => 'value' ]; $response = $http->post('http://some.url', $params, $headers);
Request and response formats
By default, response value is string and request value is array. If your service always return specific format (for example - json), you may pass those formats in constructor
use Sfadless\Utils\Http\Http; use Sfadless\Utils\Http\Format\Request\ArrayRequestFormat; use Sfadless\Utils\Http\Format\Response\JsonResponseFormat; $http = new Http( new ArrayRequestFormat(), //instanse of RequestFormatInterface, used by default new JsonResponseFormat(), //instanse of ResponseFormatInterface, response value will be passed throught getFormattedResponse method );
You may create your own formats for request and response by implementing RequestFormatInterface and ResponseFormatInterface. In request, you should create formatParams() method, in which will be passed $params (second argument for ->post() and ->get() methods), and return value for stream context option. For response format, you should implement getFormattedResponse() method, this is the layer between response from server and you code.