From 7d55213a703b0b082cbc2af3cf6595f6ae3c9515 Mon Sep 17 00:00:00 2001 From: vickzkater Date: Wed, 16 Sep 2020 14:42:22 +0700 Subject: [PATCH] Add function guzzle_post_multipart() - Guzzle POST file with Auth --- README.md | 3 + app/Http/Controllers/Controller.php | 118 +++++++++++++++++++--------- 2 files changed, 84 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index b2a7bd4..4fa74a0 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ Developed by [@vickzkater](https://github.com/vickzkater/) (Powered by [KINIDI T - [x] Support Session Driver Database (please check section `Session Driver Database`) - [x] Security update: if password has been changed, then force user to re-login - [x] Feature logout from all sessions +- [x] Sample function sending email & email template (HTML & Plain Text) ## Admin Panel @@ -77,6 +78,8 @@ Developed by [@vickzkater](https://github.com/vickzkater/) (Powered by [KINIDI T ## Version +***Current Version: 2.0.3 (Laravel 7.28.2)** + Laravel | Lara-S-CMS :---------|:---------- 5.8.x | 1.0 ; 1.1.0 diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 793d08e..a064aa1 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -142,6 +142,55 @@ public function __construct() }); } + /** + * Guzzle GET Public Access (without token Authorization Bearer) + * + * @param String $url (Target API URL) required + * @param Array $auth (htaccess auth) optional + * + * @return Object (API Response) + */ + protected function guzzle_get_public($url, $auth = null) + { + $config = ['http_errors' => false]; + if (env('APP_DEBUG')) { + $config['verify'] = false; + } + if ($auth) { + $config['auth'] = $auth; + } + $client = new Client($config); + $request = $client->request('GET', $url); + $response = $request->getBody(); + + return json_decode($response); + } + + /** + * Guzzle POST Public Access (without token Authorization Bearer) + * + * @param String $url (Target API URL) required + * @param Array $parameter (Paramaters) required + * @param Array $auth (htaccess auth) optional + * + * @return Object (API Response) + */ + protected function guzzle_post_public($url, $parameter, $auth = null) + { + $config = ['http_errors' => false]; + if (env('APP_DEBUG')) { + $config['verify'] = false; + } + if ($auth) { + $config['auth'] = $auth; + } + $client = new Client($config); + $request = $client->request('POST', $url, ['form_params' => $parameter]); + $response = $request->getBody()->getContents(); + + return json_decode($response); + } + /** * Guzzle GET with token Authorization Bearer * @@ -204,51 +253,46 @@ protected function guzzle_post($url, $token, $parameter, $auth = null) } /** - * Guzzle GET Public Access (without token Authorization Bearer) - * - * @param String $url (Target API URL) required - * @param Array $auth (htaccess auth) optional - * - * @return Object (API Response) - */ - protected function guzzle_get_public($url, $auth = null) - { - $config = ['http_errors' => false]; - if (env('APP_DEBUG')) { - $config['verify'] = false; - } - if ($auth) { - $config['auth'] = $auth; - } - $client = new Client($config); - $request = $client->request('GET', $url); - $response = $request->getBody(); - - return json_decode($response); - } - - /** - * Guzzle POST Public Access (without token Authorization Bearer) + * Guzzle POST file with token Authorization Bearer * * @param String $url (Target API URL) required + * @param String $token (Token Authorization Bearer) required * @param Array $parameter (Paramaters) required * @param Array $auth (htaccess auth) optional * * @return Object (API Response) */ - protected function guzzle_post_public($url, $parameter, $auth = null) + // *Sample: + // // Set params + // $params = [ + // [ + // 'name' => 'user_id', + // 'contents' => $user_id + // ], + // [ + // 'name' => 'avatar', + // 'contents' => fopen($request->file('avatar')->getRealPath(), "r"), + // 'filename' => $request->file('avatar')->hashName() + // ] + // ]; + protected function guzzle_post_multipart($url, $token, $parameter, $auth = null) { - $config = ['http_errors' => false]; - if (env('APP_DEBUG')) { - $config['verify'] = false; - } - if ($auth) { - $config['auth'] = $auth; - } - $client = new Client($config); - $request = $client->request('POST', $url, ['form_params' => $parameter]); - $response = $request->getBody()->getContents(); + if (empty($token)) { + return 'Unauthorized'; + } else { + $config = ['http_errors' => false]; + if (env('APP_DEBUG')) { + $config['verify'] = false; + } + if ($auth) { + $config['auth'] = $auth; + } + $client = new Client($config); + $headers = array('Authorization' => 'bearer ' . $token); + $request = $client->request('POST', $url, ['headers' => $headers, 'multipart' => $parameter]); + $response = $request->getBody()->getContents(); - return json_decode($response); + return json_decode($response); + } } }