$response = Http::withHeaders([
"Accept-Language" => "en_US",
'Accept' => 'application/x-www-form-urlencoded',
])
->post('https://domain.com/api/token', [
'code' => '...',
'grant_type' => '...',
'client_id' => '...',
'client_secret' => '...',
'redirect_uri' => '...'
]);
Apr 23, 2021
14
Level 3
HTTP Facade
Hi guys. Now I'm introducing in API requests and it's stuff and I'm very exciting. Doing this
$data = http_build_query([
'grant_type' => 'authorization_code',
'code' => $request->code,
'redirect_uri' => 'http://...',
'client_id' => '...',
'client_secret' => '...',
]);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $data
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://api.....', false, $context);
This work, I see there is some other ways to do it more easy and more efficient. But I'm looking through the Http Facade and the Laravel documentation for achieve the same result with before code. I try this but isn't right
$response = Http::withHeaders([
'Content-Type' => 'application/x-www-form-urlencoded'
])->withOptions([
'grant_type' => 'authorization_code',
'code' => $request->code,
'redirect_uri' => 'http://...',
'client_id' => '....',
'client_secret' => '....',
])->post('https://api.....');
some help with?
Level 27
So sorry, more used to guzzle rather than Http::
This should sort it out.
$response = Http::asForm()->post('https://domain.com/api/token', [
'code' => '...',
'grant_type' => '...',
'client_id' => '...',
'client_secret' => '...',
'redirect_uri' => '...',
]);
1 like
Please or to participate in this conversation.