我们在 Laravel Lumen 中实现了一个系统:
完整流程图: https://drive.google.com/file/d/1rFdkUwUaTQ4DGQaxXveu6CFylg1o1oLN/view?usp=sharing
假设客户端点击了我们在服务器端定义的 POST API 1,并且必须在服务器端使用该 POST API 1 响应。 [注意:在此 POST API 1 事件中,我们必须自动触发另一个 POST API 2,但在返回 202之后。]
现在作为服务器,我们必须向客户端发送响应作为确认,就像“是的,我们收到了您的 API 命中”。以代码的形式,我们将返回状态代码 = 202 以及消息 = Request Accepted for POST API 1。
现在POST API 1返回202,所以POST API 2必须在服务器端触发,在body中服务器将使用POST API 1响应数据
到这里流程就完成了。
api.php 文件
<?php /** @var \Laravel\Lumen\Routing\Router $router */ /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It is a breeze. Simply tell Lumen the URIs it should respond to | and give it the Closure to call when that URI is requested. | */ // clients will call this API $router->group(['prefix' => 'api'], function () use ($router) { $router->post('API1', ['uses' => 'HIP\GatewayApi@api1']); });
GatewayApi.php
namespace App\Http\Controllers\HIP; use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Queue; use App\Jobs\Job; public function api1(){ $data = Request::all(); // here we use event, queues, jobs, but didnt work, i m showing you one of the example of it Queue::push(new Job($data)); return response()->json(['message' => 'Request Accepted'], 202); } public function API2(){ // server will call the client api and client will return 202 reponse }
工作.php
<?php namespace App\Jobs; use App\Http\Controllers\HIP\GatewayApi; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Log; use App\Events\OnCallBackApi; class ProcessAbhaCallback implements ShouldQueue { use InteractsWithQueue, SerializesModels; protected $data; /** * Create a new job instance. * * @param array $data * @return void */ public function __construct(array $data) { $this->data = $data; } /** * Execute the job. * * @return void */ public function handle() { event(new OnCallBackApi($this->data)); } }
OnCallBackApi.php
<?php namespace App\Events; class OnCallBackApi extends Event { /** * Create a new event instance. * * @return void */ public $event; public function __construct($event) { $this->event = $event; } }
SendOnCallBackApi.php ---> 监听器
<?php namespace App\Listeners; use App\Events\OnCallBackApi; use App\Http\Controllers\HIP\GatewayApi; use Log; class SendOnCallBackApi { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param OnCallBackApi $event * @return void */ public function handle(OnCallBackApi $event) { $api = new GatewayApi(); $api->API2($data); } }
因此请尝试执行与上述相同的操作,但首先命中 API2,然后 API 1 返回 202 状态。
想要API 1先返回202,然后API 2需要由服务器命中到客户端
您可以发送 api1 响应 202,然后发送 api2 响应,如下所示:
public function api1(){
$data = Request::all();
//send the api1 response to client.
response()->json(['message' => 'Request Accepted'], 202)->send();
//send the api2 response
Queue::push(new Job($data));
}