yii2中更新后的多个事件

yii2中更新后的多个事件,yii2,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(); $mo

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

$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]);
使用此代码,用户将收到sms通知。但是,有一个来自服务提供商的返回代码显示为输出。最后一段重定向到视图页面的代码没有被读取。我必须在更新后添加此通知代码的多个块。请让我知道处理这种情况的最佳方法。我还想在更新后显示视图,用户应该得到通知

使用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"
        }
    ]
}

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

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

对于Curl调用,可以使用易于使用的Yii2库

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

  • 首先通过键入
    $sudo apt get Install CURL安装CURL
  • 然后键入
    $sudo服务apache2 Restart
  • 然后通过键入
    $sudo apt get Install PHP5 CURL安装PHP5 CURL
  • 将提示安装。。。键入
    y
    yes
  • 然后通过键入
    $sudo service apache2 Restart
    Done来重新启动Apache
使用
linslin/curl
的简单curl
get
请求如下所示

// 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;
    }
} 

为什么不使用
curl
将请求发送到api,而不是使用
return redirect
下一行将永远不会执行,您可以使用库进行curl调用。尝试一下…查看下面的答案这很奇怪,我刚刚运行了您的代码,收到了api针对虚拟手机号码发出的响应
386545766e4a3939334313430
,您可以删除扩展名并运行
composer update
,然后使用
composer require--preference dist linslin/yii2 curl“*”
再次安装它。您是否能够运行我在下面添加的示例请求,或者它在我的答案上也给出了相同的错误。?获取错误-调用未定义的方法linslin\yii2\curl\curl::setGetParams(),您安装了扩展吗@Tanmay这真的很奇怪,你能用更新后使用的确切代码更新你的问题吗@坦美