单纯纪录自己用的 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));}