在yii2中发送短信

时间:2016-09-01 13:28:30

标签: php yii2 yii2-advanced-app

我在一个控制器中使用两个模型。其中一个是数据库模型(模型)另一个模型用于发送短信(smsModel)

我在smsModel中遇到问题。

我的结果中出现此错误:

Class 'fcadmin\models\SoapClient' not found

我该如何解决?

我的控制器:

public function actionCreate($id) {

        $model = new Requestresult();
        $smsModel = new SmsSender();
        $request_model = Request::findOne(['id' => $id]);

        $model->CodeKargah = $request_model->CodeKargah;
        $model->month = $request_model->month;
        $model->trackingCode = $request_model->trackingCode;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $smsModel->sendSms('09193452126', 'sdf');
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

smsModel:

public function sendSms($to, $text) {
    $options = [
        'login' => 'myusername',
        'password' => 'mypassword'
    ];
    $client = new SoapClient('http://sms.hostiran.net/webservice/?WSDL', $options);
    $messageId = $client->send($to, $text);
    sleep(3);
    return ($client->deliveryStatus($messageId));
}

1 个答案:

答案 0 :(得分:3)

您需要了解有关命名空间的信息。如果您在命名空间中并且不告诉PHP您要使用全局命名空间,它将在当前命名空间中查找名称为x的类。

在您的情况下,您需要使用new \SoapClient