使用put / post将数据发送到Laravel中的Web服务

时间:2018-02-04 21:45:25

标签: php laravel web-services guzzle

我正在尝试创建一个动态方法,使用guzzle和laravel通过我的webservice发送/接收我的数据

我的控制器中有以下结构

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)
    {
        //
    }
}

在此之前,我的IndexEdit方法完美无缺,因为发送方法是GET。现在我尝试使用PUT方法向服务器发送请求,但我没有收到。

AppWebService方法将第一个参数作为我访问的路由的名称,我要传递的类型和信息。

我试过这种方式

public function update(Request $request, $id)
{
    $Dados = $this->AppWebService($this->Tabela.'/'.$id, 'PUT', ['data'=>'$request']); 
    dd($Dados);  
}

,错误是

  

(1/1)InvalidArgumentException URI必须是字符串或UriInterface

我的客户收到的请求是

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"
}

因为我的应用程序也在laravel和我的webservice中,我以这种方式发送请求是否有任何问题?或者我必须提取并发送它? 以及如何将put和post方法发送到我的动态函数?

[EDIT]文件Web服务,路由和控制器

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');            

        });     
    }); 
});

接收我的请求的Web服务方法

class TipoProjetoController extends Controller
{
  public function Update(Request $request, $id){
      return 'Change data of id:'.$id;
  }

}

1 个答案:

答案 0 :(得分:1)

尝试使用['data'=>$request]代替['data'=>'$request']

您还使用$Client->request();错误。第二个参数应该是URI,而不是数组,因此你的错误。

此外,基本URI不应该以这种方式使用, 所以请尝试

$Client = new Client();
$url = config('constants.caminhoWS').$Who;    
$response = $Client->request($Type,$url, $Data);

(如果您要使用基本URI,请使用$Client->request()中的第二个参数传递其余的网址,例如

$Client = new Client(['base_uri' => config('constants.caminhoWS')]);  
$response = $Client->request($Type, $Who, $Data);

然后Guzzle将构建URL ... http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request