编辑:
这可能不是最好的方法。但这就是我当时所做的。我尝试 curl 和 guzzle 使用会话 cookie 和标头中的所有内容构建请求,使其看起来像来自 Web 浏览器的请求。无法让它工作。
我将 Web 套接字的通道 ID 用于我希望发生更改并将其与其他内容连接起来的浏览器,然后使用 encrypt($string) 对其进行加密。之后,我使用加密字符串生成二维码。
移动应用程序(已作为经过身份验证的用户登录)扫描它并使用该 QR 字符串和其他数据发出发布请求。 Passport 负责此请求的身份验证部分。解密 QR 字符串后,我得到了网络套接字的频道 ID。
然后我在该频道中使用适当的事件和数据进行广播。在浏览器中捕获该广播并使用 JavaScript 重新加载该页面。
/*... processing other data ...*/
$broadcastService = new BroadcastService();
$broadcastService->trigger($channelId, $eventName, encrypt($$data));
/*... returned response to the mobile app...*/
我的广播服务:
namespace App\Services;
use Illuminate\Support\Facades\Log;
use Pusher\Pusher;
use Pusher\PusherException;
class BroadcastService {
public $broadcast = null;
public function __construct() {
$config = config('broadcasting.connections.pusher');
try {
$this->broadcast = new Pusher($config['key'], $config['secret'], $config['app_id'], $config['options']);
} catch (PusherException $e) {
Log::info($e->getMessage());
}
}
public function trigger($channel, $event, $data) {
$this->broadcast->trigger($channel, $event, $data);
}
}
在我看来:
<script src="{{asset('assets/js/pusher.js')}}"></script>
<script src="{{asset('assets/js/app.js')}}" ></script>
<script>
<?php
use Illuminate\Support\Facades\Cookie;
$channel = 'Channel id';
?>
Echo.channel('{{$channel}}')
.listen('.myEvent' , data => {
// processing data
window.location.reload();
});
</script>
我为此使用了Laravel Echo。
同样,这不是最好的方法。这对我来说正好适用于该特定功能。
可能有很多更好的方法可以做到这一点。如果有人知道更好的方法,请告诉我。
据我了解,您希望通过 REST 实现用户创建和身份验证。然后从数据库中检索数据。如果我错了,请纠正我。
而且我猜您已经知道如何使用令牌通过 API 进行通信。你只是被如何用 laravel 实现它所困。
您可以使用 Laravel Passport 进行身份验证部分。它有很好的documentation。
另外,利用这个medium article。它将帮助您逐步完成整个过程。