laravel example http post request
Example 1: laravel send http post request json
use GuzzleHttp\Client;
class yourController extends Controller {
public function saveApiData()
{
$client = new Client();
$res = $client->request('POST', 'https://url_to_the_api', [
'form_params' => [
'client_id' => 'test_id',
'secret' => 'test_secret',
]
]);
echo $res->getStatusCode();
echo $res->getHeader('content-type');
echo $res->getBody();
}
Example 2: laravel 6 make http request
$client = new GuzzleHttp\Client();
$res = $client->get('https://api.github.com/user', ['auth' => ['user', 'pass']]);
echo $res->getStatusCode();
echo $res->getBody();
Example 3: check method in laravel
if ($request->isMethod('post')) {
return ['user' => $user];
}