iT邦幫忙

0

在 header 帶 token 的 Guzzle Http Request 模板

  • 分享至 

  • xImage
  •  

單純紀錄自己用的 Guzzle Request 模板

簡單版

use GuzzleHttp\Client;

$client = new GuzzleHttp\Client(['base_uri' => 'https://api.example/']);

$token = '<TOKEN>';
$method = 'GET';
$url = 'v1.3/products';
$headers = ['Authorization' => 'Bearer ' . $token];//$headers must be an array

$response = $client->request($method, $url, [
		'headers' => $headers
]);

dd(json_decode($response->getBody(),true));

try & catch 版

use GuzzleHttp\Client as Client;
use GuzzleHttp\Psr7\Request as Request;
use GuzzleHttp\Exception\ClientException as ClientException;

$client = new Client(['base_uri' => 'https://api.example/']);

$token = '<TOKEN>'
$method = 'POST';
$url = 'URL'
$header = ['Authorization'=>'Bearer '.$token, 'Content-Type'=>'application/json'];
$body = 'BODY';

$request = new Request($method, $url, $header, $body);

try{
	$response = $client->send($request);
	dd(json_decode($response->getBody(),true));
} catch (ClientException $e){
	dd(json_decode($e->getResponse()->getBody(),true));
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言