在yii2中更新后的多个事件

时间:2018-05-31 15:59:46

标签: yii2

每当行更新时,我都希望通过短信通知用户。我添加了以下代码 -

$transaction->commit();
$mobiletemp = User::find()->leftJoin('auth_assignment', 'auth_assignment.user_id = user.id')->select('mobileno')->andWhere(['auth_assignment.item_name' => 'c_apo'])->column();
$mobile = implode(",", $mobiletemp);
return $this->redirect('http://api.msg91.com/api/sendhttp.php?' . http_build_query(['sender'=>'TSTMSG', 'route'=>'4', 'mobiles'=> $mobile, 'authkey'=>'My Auth Key', 'country'=>'0','message'=>('Dear Sir, WP '. $wpno. ' is waiting for your approval. Please take necessary steps. Thank You.')]));
return $this->redirect(['view', 'id' => $model->wp_no]);

使用此代码,用户将收到短信通知。但是,有一个来自服务提供商的返回码,它被显示为输出。并且重定向到视图页面的最后一段代码没有被读取。我必须在更新后添加此通知代码的多个块。请让我知道处理这种情况的最佳方法是什么。我想在更新后显示视图,用户应该收到通知。

使用curl后我现在的代码

if ($flag) {
    $transaction->commit();
    $mobiletemp = User::find()->leftJoin('auth_assignment', 'auth_assignment.user_id = user.id')->select('mobileno')->andWhere(['auth_assignment.item_name' => 'c_apo'])->column();
    $mobile = implode(",", $mobiletemp);
    $curl = new curl\Curl();
    $response = $curl->setGetParams(['sender'=>'TSTMSG', 'route'=>'4', 'mobiles'=> $mobile, 'authkey'=>'My Auth Code', 'country'=>'0','message'=>('Dear Sir, WP '. $wpno. ' is waiting for your approval. Please take necessary steps. Thank You.')])->get('http://api.msg91.com/api/sendhttp.php');
    if ($curl->errorCode === null) {
         return $this->redirect(['view', 'id' => $model->wp_no]);
    }else {
         // List of curl error codes here https://curl.haxx.se/libcurl/c/libcurl-errors.html
        switch ($curl->errorCode) {

            case 6:
                //host unknown example
                break;
        }
    }
}

composer.json

{
    "name": "yiisoft/yii2-app-advanced",
    "description": "Yii 2 Advanced Project Template",
    "keywords": ["yii2", "framework", "advanced", "project template"],
    "homepage": "http://www.yiiframework.com/",
    "type": "project",
    "license": "BSD-3-Clause",
    "support": {
        "issues": "https://github.com/yiisoft/yii2/issues?state=open",
        "forum": "http://www.yiiframework.com/forum/",
        "wiki": "http://www.yiiframework.com/wiki/",
        "irc": "irc://irc.freenode.net/yii",
        "source": "https://github.com/yiisoft/yii2"
    },
    "minimum-stability": "stable",
    "require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": "~2.0.6",
        "yiisoft/yii2-bootstrap": "~2.0.0",
        "yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0",
        "kartik-v/yii2-widget-sidenav": "*",
        "kartik-v/yii2-widget-activeform": "@dev",
        "kartik-v/yii2-widget-datepicker": "@dev",
        "kartik-v/yii2-widgets": "*",
        "wbraganca/yii2-dynamicform": "dev-master",
        "kartik-v/yii2-grid": "@dev",
        "mpdf/mpdf": "@dev",
        "kartik-v/yii2-mpdf": "*",
        "linslin/yii2-curl": "*"
    },
    "require-dev": {
        "yiisoft/yii2-debug": "~2.0.0",
        "yiisoft/yii2-gii": "~2.0.0",
        "yiisoft/yii2-faker": "~2.0.0",
        "codeception/base": "^2.2.3",
        "codeception/verify": "~0.3.1"
    },
    "config": {
        "process-timeout": 1800
    },
    "repositories": [
        {
            "type": "composer",
            "url": "https://asset-packagist.org"
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

您可以使用curl将请求发送到API,然后使用

return $this->redirect(['view', 'id' => $model->wp_no]);

对于Curl调用,您可以将linslin库用于Yii2,这非常容易使用。

  

只需确保已安装cURL或执行以下步骤即可   安装cURL。

     
      
  • 首先输入$ sudo apt-get install curl
  • 安装CURL   
  • 然后键入$ sudo service apache2 restart
  • 重启Apache   
  • 然后输入$ sudo apt-get install php5-curl
  • 安装PHP5 CURL   
  • 将提示安装...键入yyes
  •   
  • 然后输入$ sudo service apache2 restart完成!
  • 重启Apache   

使用get的简单curl linslin/Curl请求将如下所示。

// GET request with GET params
// http://example.com/?key=value&scondKey=secondValue
$curl = new curl\Curl();
$response = $curl->setGetParams([
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->get('http://example.com/');

所以你的代码看起来像这样

$transaction->commit();
$mobiletemp = User::find()->leftJoin('auth_assignment', 'auth_assignment.user_id = user.id')->select('mobileno')->andWhere(['auth_assignment.item_name' => 'c_apo'])->column();
$mobile = implode(",", $mobiletemp);
$curl = new curl\Curl();
$response = $curl->setGetParams(['sender'=>'TSTMSG', 'route'=>'4', 'mobiles'=> $mobile, 'authkey'=>'My Auth Key', 'country'=>'0','message'=>('Dear Sir, WP '. $wpno. ' is waiting for your approval. Please take necessary steps. Thank You.')])
         ->get('http://api.msg91.com/api/sendhttp.php');

if ($curl->errorCode === null) {
     return $this->redirect(['view', 'id' => $model->wp_no]);
} else {
     // List of curl error codes here https://curl.haxx.se/libcurl/c/libcurl-errors.html
    switch ($curl->errorCode) {

        case 6:
            //host unknown example
            break;
    }
}