For Userfrosting >4.1, in your sprinkle/composer.json file, add a requirement to include Guzzle:
"require": {"guzzlehttp/guzzle": "~6.0"}
(remember to run composer update
to install the new dependency.
Guzzle Docs
Then in your controller include guzzle:
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;
then you can initiate guzzle with:
$client = new Client([
'base_uri' => $config['api']['host'].'/',
'timeout' => 5 // your timeout param
]);
(I set my api host in the sprinkle config, using an environment variable, so its not hardcoded.)
you can then make a POST request as follows, returning the response into a variable.
$api_response = $client->post('your_api_route', [
'json' => [
'api_param_1' => 'Hello',
'api_param_2' => 'World!'
]
]);
Also reccomended the wrap the last bit in a try
and catch an guzzle/http exceptions.
If your response is a JSON document, you can retrieve the contents into an array with:
$data = json_decode($api_response->getBody()->getContents(), true);