使用异步的方式解决执行慢等待的问题

2022-04-14 14:50:22 阅读:2 编辑

在public/async目录创建index.php

<?php
$newurl = $_REQUEST["newurl"];
$newurl = urldecode($newurl);
print_r($newurl);
new_curl_get($newurl);
function new_curl_get($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60*10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $output = curl_exec($ch);
    curl_close($ch);
    print_r($output);
}

调用异步

public function handleAozhiCommissionAsync($order_id, $type, $user_id, $flag)
    {
        global $_W;

        $host = $_W["siteroot"];
        $host = str_replace("https://","http://",$host);

        $url =$host."async/index.php";
        $spec_sign = md5($order_id.$type.$user_id);

        $newurl =$_W["siteroot"]."app/index.php?i=".$_W["uniacid"]."&t=0&v=1.1&from=wxapp&c=entry&a=wxapp&do=Api_aozhi|handleCommissionAsync&order_id={$order_id}&type={$type}&user_id={$user_id}&flag={$flag}&spec_sign={$spec_sign}";
        logging_run("url:".$url);
        $param = [
            "newurl"=>urlencode($newurl),
        ];
        $this->post_without_wait($url,$param);
    }
实际接口
public function handleCommissionAsync()
    {
        ini_set('max_execution_time', '0');
        set_time_limit(0);
        ini_set('memory_limit', '1024M');
        logging_run("handleCommissionAsync");
        //$order_id, $type, $user_id, $flag
        $order_id = input("request.order_id");
        $type = input("request.type");
        $user_id = input("request.user_id");
        $flag = input("request.flag");
        $spec_sign = input("request.spec_sign");
        if(md5($order_id.$type.$user_id) == $spec_sign){
            $res = (new CommonCommission())->handleAozhiCommission($order_id, $type, $user_id, $flag);
        }
        logging_run("handleCommissionAsync finish");
        success_json();
    }

nginx的配置修改

修改前:
 if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
修改后:
 set $flag "0";
    if ($server_port !~ 443){
        set $flag "${flag}1";
    }
    if ($request_uri !~ "/async"){
        set $flag "${flag}2";
    }
    if ($flag = "012")
    {
        rewrite ^(/.*)$ https://$host$1 permanent;
    }