【问题标题】:Sending data with put / post to a webservice in Laravel使用 put / post 将数据发送到 Laravel 中的网络服务
【发布时间】:2018-07-14 18:17:43
【问题描述】:

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

我的控制器中有以下结构

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 和我的 web 服务中,我以这种方式发送请求有什么问题吗?还是我必须提取并发送它? 以及如何将 put 和 post 方法发送到我的动态函数?

[编辑] 文件 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');            

        });     
    }); 
});

接收我的请求的Webservice方法

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

}

【问题讨论】:

  • 你的意思是['data'=>$request] 而不是['data'=>'$request'] 吗?
  • 关于这个$Client = new Client(['base_uri' => config('constants.caminhoWS').$Who]);你能把config('constants.caminhoWS').$Who提取到它自己的变量中并转储它,确保它是正确的吗?
  • 我们可以看看你的路线文件吗?
  • 我编辑了问题并提出,如果不是很清楚,我可以提供更多的东西,以便您可以帮助我,如果可能的话
  • webservice上URL的路由文件呢? (当您点击更新方法时,URL guzzle 正在 PUTting)

标签: php laravel web-services guzzle


【解决方案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() 中的第二个参数来传递 URL 的其余部分,例如

$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

【讨论】:

  • 这给出了一个新错误,(1/1) InvalidArgumentException URI 必须是字符串或 UriInterface
  • 导致405 Method Not Allowed 响应:
  • 方法 PUT 是否在您的控制器操作的路由中列出?
  • 我要添加路由和webservice方法
  • 我刚刚在我的 web 服务中评论了这一行 // // App \ Http \ Middleware \ VerifyCsrfToken :: class 文件,它工作了,但如果我再把它放进去,问题就出现了跨度>
猜你喜欢
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
相关资源
最近更新 更多