问题
I'm trying to create a dynamic method to send / receive my data with my webservice using guzzle and laravel
I have the following structure in my controller
class TipoProjetoController extends Controller
{
private $Tabela = 'tipoprojeto';
public function AppWebService($Who, $Type, $Data)
{
$Client = new Client(['base_uri' => config('constants.caminhoWS').$Who]);
$response = $Client->request($Type, $Data);
return $response->getBody()->getContents();
}
public function index()
{
$Dados = $this->AppWebService($this->Tabela,'GET','');
$Titulo = 'Tipos de Projeto';
$jsonObj = json_decode($Dados);
$Obj = $jsonObj->data;
return view('Painel.TipoProjeto.index',compact('Obj','Titulo') );
}
public function create()
{
return view('Painel.TipoProjeto.create-edit');
}
public function store(Request $request)
{
//
}
public function show($id)
{
//
}
public function edit($id)
{
$Dados = $this->AppWebService($this->Tabela.'/'.$id,'GET','');
if( $Dados == null )
return redirect()->route('TipoProjeto.index');
else
{
$Objeto = json_decode($Dados);
return view('Painel.TipoProjeto.create-edit',compact('Objeto'));
}
}
public function update(Request $request, $id)
{
$Dados = $this->AppWebService($this->Tabela.'/'.$id, 'PUT', '');
dd($Dados);
}
public function destroy($id)
{
//
}
}
Until then everything worked perfectly in my Index
and Edit
method, because the method of sending is GET
. now I am trying to send a request to the server with the PUT
method and I am not getting.
The AppWebService method has as the first parameter the name of the route that I am accessing, the type and the information that I am going to pass.
I tried this way
public function update(Request $request, $id)
{
$Dados = $this->AppWebService($this->Tabela.'/'.$id, 'PUT', ['data'=>'$request']);
dd($Dados);
}
and the error was
(1/1) InvalidArgumentException URI must be a string or UriInterface
my request received from my client was
dd($request)
Request {#38 ▼
#json: null
#convertedFiles: null
#userResolver: Closure {#154 ▶}
#routeResolver: Closure {#156 ▶}
+attributes: ParameterBag {#40 ▶}
+request: ParameterBag {#39 ▶}
+query: ParameterBag {#46 ▶}
+server: ServerBag {#42 ▶}
+files: FileBag {#43 ▶}
+cookies: ParameterBag {#41 ▶}
+headers: HeaderBag {#44 ▶}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/Painel/TipoProjeto/1"
#requestUri: "/index.php/Painel/TipoProjeto/1"
#baseUrl: "/index.php"
#basePath: null
#method: "PUT"
#format: null
#session: Store {#185 ▶}
#locale: null
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
basePath: ""
format: "html"
}
as my application is in laravel and my webservice too, is it any problem that I send the request this way? or do I have to extract and send it? and how do I send the put and post methods to my dynamic function?
[EDIT] File Web Service, Route and Controller
Route::group(['prefix' => 'api'],function(){
Route::group(['prefix' => 'user'], function(){
Route::group(['prefix' => 'tipoprojeto'], function(){
Route::get('/','Painel\TipoProjetoController@All');
Route::get('{id}','Painel\TipoProjetoController@Get');
Route::post('','Painel\TipoProjetoController@Save');
Route::put('{id}','Painel\TipoProjetoController@Update');
Route::delete('{id}','Painel\TipoProjetoController@Delete');
});
});
});
Webservice method that receives my request
class TipoProjetoController extends Controller
{
public function Update(Request $request, $id){
return 'Change data of id:'.$id;
}
}
回答1:
Try with ['data'=>$request]
instead of ['data'=>'$request']
you're also using $Client->request();
wrong. The second parameter should be the URI, not an array, hence your error.
Also the base URI is not meant to be used that way, so instead try
$Client = new Client();
$url = config('constants.caminhoWS').$Who;
$response = $Client->request($Type,$url, $Data);
(if you are going to use a base URI, use the second parameter in $Client->request()
to pass the rest of the URL, like
$Client = new Client(['base_uri' => config('constants.caminhoWS')]);
$response = $Client->request($Type, $Who, $Data);
Then Guzzle will construct the URL... http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request
来源:https://stackoverflow.com/questions/48613305/sending-data-with-put-post-to-a-webservice-in-laravel