使用put/post将数据发送到Laravel中的Web服务 [英] Sending data with put / post to a webservice in Laravel

查看:211
本文介绍了使用put/post将数据发送到Laravel中的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一种动态方法来使用guzzle和laravel通过我的Web服务发送/接收数据

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

直到所有事情都在我的IndexEdit方法中完美运行,因为发送的方法是GET.现在我正在尝试使用PUT方法向服务器发送请求,但没有得到.

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.

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

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.

我尝试过这种方式

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

错误是

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

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

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

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?

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

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方法

Webservice method that receives my request

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

}

推荐答案

尝试使用['data'=>$request]而不是['data'=>'$request']

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

you're also using $Client->request(); wrong. The second parameter should be the URI, not an array, hence your error.

另外,基本URI也不打算那样使用, 因此,请尝试

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

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

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

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

Then Guzzle will construct the URL... http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request

这篇关于使用put/post将数据发送到Laravel中的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆