在 yii2 中发送短信

Sending SMS in yii2

我在一个控制器中使用两个模型。其中一个是数据库模型 (model) 另一个模型是用于发送短信 (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,
            ]);
        }
    }

短信模型:

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

您需要阅读有关命名空间的内容。如果您在命名空间中并且没有告诉 PHP 您想要使用全局命名空间,它将在当前命名空间中查找名称 x 的 类。

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