我是PHP的新手,并且具有使用RESTful API的纯正经验。因此,如果有人可以帮助解决我的问题,我将不胜感激。
目标是在Matrix中创建一个聊天室,执行一些基本步骤,例如注册,登录以及通过该聊天室进行一对客户之间的进一步通信。
问题1:看起来这是互联网上唯一可用的唯一文档。https://matrix.org/docs/guides/client-server.html
对我来说还不清楚。找不到我需要的任何其他好的示例。
问题2:不确定使用哪种类型的请求,据我了解,RESTful API可以使用其中的许多类型:简单请求,异步请求,并发请求,并且语法当然有所不同。
问题3:不确定我是否正确地提出要求。下面的例子。
我已经完成的工作:因为我的项目在Laravel上,所以我使用Guzzle客户端来构成我的查询,并且通过作曲家已经安装了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();
}
);
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>
路线/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,发现在创建会议室,开始聊天,邀请其他用户等方面,所有内容都得到了很好的记录。
玩得开心!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句