+ Fix bugs while upload files
This commit is contained in:
parent
9878189ee9
commit
b921c059eb
|
@ -6,7 +6,6 @@ namespace App\Http\Controllers;
|
||||||
use App\Services\WalletService;
|
use App\Services\WalletService;
|
||||||
use App\Traits\ApiResponser;
|
use App\Traits\ApiResponser;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
|
||||||
|
|
||||||
class WalletServiceController extends Controller
|
class WalletServiceController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -40,6 +39,13 @@ class WalletServiceController extends Controller
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function postWithFiles(Request $request)
|
||||||
|
{
|
||||||
|
return $this->successResponse($this->walletService->postFiles(
|
||||||
|
substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all(),$request->header()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
public function put(Request $request)
|
public function put(Request $request)
|
||||||
{
|
{
|
||||||
return $this->successResponse($this->walletService->put(
|
return $this->successResponse($this->walletService->put(
|
||||||
|
|
|
@ -29,6 +29,11 @@ class WalletService
|
||||||
return $this->perfomRequest('POST',$uri,$data,$headers);
|
return $this->perfomRequest('POST',$uri,$data,$headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function postFiles($uri , $data, $headers)
|
||||||
|
{
|
||||||
|
return $this->perfomRequestWithFiles('POST',$uri,$data,$headers);
|
||||||
|
}
|
||||||
|
|
||||||
public function get($uri , $data, $headers)
|
public function get($uri , $data, $headers)
|
||||||
{
|
{
|
||||||
return $this->perfomRequest('GET',$uri,$data,$headers);
|
return $this->perfomRequest('GET',$uri,$data,$headers);
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Traits;
|
namespace App\Traits;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Psr7\Request;
|
||||||
|
use GuzzleHttp\Psr7\MultipartStream;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
|
|
||||||
trait ConsumesExternalService
|
trait ConsumesExternalService
|
||||||
|
@ -21,37 +24,50 @@ trait ConsumesExternalService
|
||||||
'base_uri' => $this->baseUri,
|
'base_uri' => $this->baseUri,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if(isset($this->key)){
|
if (isset($this->key)) {
|
||||||
unset($headers['authorization']);
|
unset($headers['authorization']);
|
||||||
$headers['Authorization'] = $this->key;
|
$headers['Authorization'] = $this->key;
|
||||||
}
|
}
|
||||||
$contentType = $headers['content-type'][0];
|
|
||||||
|
|
||||||
if(strpos($contentType, 'multipart/form-data') !== false){
|
$response = $client->request($method, $requestUrl, ['json' => $body, 'form_params' => $formParams, 'headers' => $headers]);
|
||||||
// $headers['content-type'] = 'multipart/form-data';
|
|
||||||
// dd($body);
|
|
||||||
$response = $client->request($method ,$requestUrl , ['multipart' => $this->bodyToMultipart($body),'headers' => $headers]);
|
|
||||||
}else{
|
|
||||||
$response = $client->request($method , $requestUrl , ['json'=> $body ,'form_params' => $formParams , 'headers' => $headers]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $response->getBody()->getContents();
|
return $response->getBody()->getContents();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function bodyToMultipart(array $body){
|
public function perfomRequestWithFiles($method, $requestUrl, $body = [], $headers = [], $formParams = [])
|
||||||
|
{
|
||||||
|
|
||||||
|
if (isset($this->key)) {
|
||||||
|
unset($headers['authorization']);
|
||||||
|
$headers['Authorization'] = $this->key;
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = new Client([
|
||||||
|
'base_uri' => $this->baseUri,
|
||||||
|
'headers' => $headers
|
||||||
|
]);
|
||||||
|
|
||||||
|
$request = new Request($method, $requestUrl, [], new MultipartStream($this->bodyToMultipart($body)));
|
||||||
|
$response = $client->send($request);
|
||||||
|
|
||||||
|
return $response->getBody()->getContents();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function bodyToMultipart(array $body)
|
||||||
|
{
|
||||||
$multipart = [];
|
$multipart = [];
|
||||||
foreach ($body as $key => $value){
|
foreach ($body as $key => $value) {
|
||||||
if( $value instanceof UploadedFile){
|
if ($value instanceof UploadedFile) {
|
||||||
array_push($multipart , [
|
array_push($multipart, [
|
||||||
'name' => $key,
|
'name' => $key,
|
||||||
'contents' => file_get_contents($value->getRealPath()),
|
'contents' => file_get_contents($value->getRealPath()),
|
||||||
'mine-type'=>$value->getMimeType(),
|
'mine-type' => $value->getMimeType(),
|
||||||
'filename' => $value->getClientOriginalName(),
|
'filename' => $value->getClientOriginalName(),
|
||||||
// 'contents' => fopen( './test.txt', 'r' ),
|
// 'headers' => [ 'Content-type' => 'application/octet-stream']
|
||||||
'headers' => [ 'Content-type' => 'multipart/form-data']
|
// 'Content-type' => 'multipart/form-data',
|
||||||
]);
|
]);
|
||||||
}else{
|
} else {
|
||||||
array_push($multipart , [
|
array_push($multipart, [
|
||||||
'name' => $key,
|
'name' => $key,
|
||||||
'contents' => $value,
|
'contents' => $value,
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -93,7 +93,7 @@
|
||||||
// Idendification routes
|
// Idendification routes
|
||||||
$router->group(['prefix' => '/identifications'] , function () use ($router){
|
$router->group(['prefix' => '/identifications'] , function () use ($router){
|
||||||
$router->post('','WalletServiceController@post');
|
$router->post('','WalletServiceController@post');
|
||||||
$router->post('{id_identification}','WalletServiceController@post');
|
$router->post('{id_identification}','WalletServiceController@postWithFiles');
|
||||||
$router->get('{id_user}','WalletServiceController@get');
|
$router->get('{id_user}','WalletServiceController@get');
|
||||||
$router->get('verify/{user_phone}','WalletServiceController@get');
|
$router->get('verify/{user_phone}','WalletServiceController@get');
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue