Advertisement
langbung01

laravel: guzzle http request

Sep 6th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1.  $client = new Client(['base_uri' => env('BACKEND_IP') . '/api/', 'verify' => false]);
  2.         try {
  3.             $response = $client->request('get', 'get_unread_email', ['query' => ['token' => session('thai_email_token')]]);
  4.             $jsonResponse = json_decode($response->getBody()->getContents(), true);
  5.             $unreadEmail = $jsonResponse['data'];
  6.         } catch (RequestException $exception) {
  7.             $unreadEmail = 0;
  8.         }
  9.  
  10. // form_param
  11. $response = $client->request('POST', 'http://httpbin.org/post', [
  12.     'form_params' => [
  13.         'field_name' => 'abc',
  14.         'other_field' => '123',
  15.         'nested_field' => [
  16.             'nested' => 'hello'
  17.         ]
  18.     ]
  19. ]);
  20.  
  21. /***********************************************************************************************/
  22.  
  23. $client = new Client([
  24.             'base_uri' => env('BACKEND_IP') . '/api/',
  25.             'verify' => false,
  26.             'headers' => [
  27.                 'Authorization' => 'Bearer '.\request()->session()->get('accessToken')
  28.             ]
  29.         ]);
  30.  
  31.         try {
  32.             $response = $client->request('get', 'upgrade_loa_by_email');
  33.             $jsonResponse = json_decode($response->getBody()->getContents(), true);
  34.             return response()->json($jsonResponse);
  35.         } catch (ClientException $exception) {
  36.             $upgradeEmail = $exception->getMessage();
  37.             return response()->json($upgradeEmail, 400);
  38.         }
  39. /*************************************************************************************************/
  40.  
  41. //json
  42. $response = $client->request('PUT', '/put', ['json' => ['foo' => 'bar']]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement