Matrix采用Laravel和Guzzle重新提供API。请求创建

Matrix采用Laravel和Guzzle重新提供API。请求创建,laravel,api,matrix,guzzle,Laravel,Api,Matrix,Guzzle,我是PHP的新手,有使用RESTful API的纯粹经验。因此,如果有人能帮我解决问题,我将非常感激 我们的目标是在Matrix中创建一个聊天室,进行一些基本的步骤,如注册、登录以及通过聊天室在两个客户端之间进行进一步的通信 问题1: 看起来这是互联网上唯一可用的文档。 我也不清楚。找不到其他我需要的好例子 问题2: 不确定要使用哪种类型的请求,据我所知,RESTful API可以使用其中的许多类型:简单请求、异步请求、concurent请求,当然语法也不同 问题3: 不确定我是否正确地向他们

我是PHP的新手,有使用RESTful API的纯粹经验。因此,如果有人能帮我解决问题,我将非常感激

我们的目标是在Matrix中创建一个聊天室,进行一些基本的步骤,如注册、登录以及通过聊天室在两个客户端之间进行进一步的通信

问题1: 看起来这是互联网上唯一可用的文档。

我也不清楚。找不到其他我需要的好例子

问题2: 不确定要使用哪种类型的请求,据我所知,RESTful API可以使用其中的许多类型:简单请求、异步请求、concurent请求,当然语法也不同

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

我已经做过的: 由于我在Laravel上的项目,我使用Guzzle客户端形成我的查询,并且via composer已经安装了矩阵依赖性,如下所述:

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

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

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

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>
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,它似乎对创建聊天室、启动聊天会话、邀请其他用户等所有内容都有很好的记录

玩得开心

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