【问题标题】:Matrix RERSful API with Laravel and Guzzle. Request creating带有 Laravel 和 Guzzle 的 Matrix RERSful API。请求创建
【发布时间】:2018-07-31 18:05:24
【问题描述】:

我是 PHP 新手,在使用 RESTful API 方面有相当纯正的经验。因此,如果有人可以帮助解决我的问题,我将不胜感激。

我们的目标是在 Matrix 中创建一个聊天室,完成一些基本步骤,例如注册、登录以及通过该聊天室在几个客户之间进一步交流。

问题 1: 看起来这是互联网上唯一可用的文档。 https://matrix.org/docs/guides/client-server.html

这对我来说并不清楚。找不到任何其他我需要的好例子。

问题 2: 不知道使用什么类型的请求,据我了解 RESTful API 可以使用其中的许多:简单请求、异步请求、并发请求,当然语法不同。

问题 3: 不确定我是否正确地自己提出请求。下面的例子。

我已经做了什么: 因为我的项目在 Laravel 上,所以我使用 Guzzle 客户端来形成我的查询,并且通过 composer 已经安装了 Matrix 依赖项,如下所述: https://github.com/updivision/matrix-php-sdk

这是我的请求示例,当然不能正常工作:

<?php
require '../vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;


    $client = new Client();


$promise = $client->requestAsync(
    'POST',
    'http://matrix.loc:80/_matrix/client/r0/register',
    [
        'json'=>[
            'username'=>'12345',
            'password'=>'12345',
            'auth'=>[
                "type"=>"m.login.dummy"
            ],
        ]
    ]
);


  $promise->then(
    function(Response $resp){
        echo $resp->getBody();
    },
    function(RequestExcprion $e){
        echo $e->getMessage();
    }
);

【问题讨论】:

    标签: laravel api matrix guzzle


    【解决方案1】:

    SDK 封装了所有 API 调用,因此您无需通过 Guzzle 创建请求。相反,请查看 resources directory 并了解 SDK 提供的内容。让我们从注册请求开始,它在源代码中可用here

    register.blade.php

    首先创建一个简单的注册表单。

    <form action="{{ route('maxtrix.register') }}" method="post">
        {{ csrf_field() }}
    
        <input type="text" name="username">
        <input type="password" name="password">
    
        <button type="submit">Register</button>
    </form>
    

    routes/matrix.php

    为矩阵 api 请求创建路由文件。这是可选的,您可以将它们放在现有的 routes/api.php 文件中,但我更喜欢将它们分开。

    Route::post('register', 'MatrixController@register')->name('matrix.register');
    Route::get('account', 'MatrixController@account')->name('matrix.account');
    

    MatrixController.php

    创建一个新的控制器来发出矩阵 api 请求。

    class MatrixController {
    
        protected $userData;
    
        // Use dependency injection to automatically get an instance of the 
        // matrix SDK.
        public function __construct(UserData $userData)
        {
             $this->userData = $userData;
        }
    
        // The registration form created above will post to this route which
        // will make the API request to register your user.
        public function register(Request $request)
        {
            // get the data from the response
            $data = $this->userData->register($request->username, $request->password);
    
            // if successful, save the registration info
            if ($data) {
                 DB::table('matrix_regisration')->insert($data);
                 return redirect()->route('matrix.account');
            } else {
            // if failure, redirect back to the registration for with errors
                 return back()->withErrors('Failed to register');
            }
        }
    }
    

    我简化了一些事情,但这应该可以让您朝着正确的方向开始。我查看了 SDK,它似乎记录了所有内容,包括创建房间、开始聊天会话、邀请其他用户等。

    玩得开心!

    【讨论】:

      猜你喜欢
      • 2017-04-16
      • 2018-12-22
      • 2018-12-06
      • 1970-01-01
      • 2018-08-28
      • 2016-09-13
      • 2023-03-04
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多