如何同时将每个用户的API请求限制为一个?

时间:2019-01-29 20:59:40

标签: php laravel api cron-task

我创建了一个用于社交营销的网站。 服务由API提供。 当用户请求服务时,API将发送多个请求。 如何使其仅请求一次服务?

public function cron(){
    $orders = Order::where('status', 'Pending')->where('service_no', '!=', 0)->get();
    $api = ApiSetting::first();
    foreach ($orders as $order){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api->url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            "key=".$api->key."&action=add&service=".$order->service_no."&link=".$order->link."&quantity=".$order->quantity);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $server_output = curl_exec ($ch);
        $item = json_decode($server_output);
        $order->order_no = $item->order;
        $order->save;
        curl_close ($ch);
    }
}

public function cronn(){
    $orders = Order::where('order_no', '!=', 0)->where('service_no', '!=', 0)->get();
    $api = ApiSetting::first();
    foreach ($orders as $order){
        $new_curl = curl_init();
        curl_setopt($new_curl, CURLOPT_URL, $api->url);
        curl_setopt($new_curl, CURLOPT_POST, 1);
        curl_setopt($new_curl, CURLOPT_POSTFIELDS,
            "key=".$api->key."&action=status&order=".$order->order_no);
        curl_setopt($new_curl, CURLOPT_RETURNTRANSFER, true);
        $server_output = curl_exec ($new_curl);
        $item = json_decode($server_output);
        $order->start_count = $item->start_count;
        $order->remains = $item->remains;
        $order->status = $item->status;
        $order->save;
        curl_close ($new_curl);
    }
}

0 个答案:

没有答案