1.2.x-dev
1.2.9999999.9999999-devSmall library for get/post requests
The Requires
- php >=5.4
- psr/http-message ^1.0
Small library for get/post requests
composer require Sfadless/http
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');
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);
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, (*1)
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., (*2)
Small library for get/post requests