TP3.2如何加载第三方类库?加载腾讯短信sdk 报错
主要在于引入 Vendor('qcloudsms.index'); index.php后还需要use引入命名空间,不然就会报类未定义的错误。
<?php
namespace Common\Controller;
use Think\Controller;
use Qcloud\Sms\SmsSingleSender;
use Qcloud\Sms\SmsMultiSender;
use Qcloud\Sms\SmsVoiceVerifyCodeSender;
use Qcloud\Sms\SmsVoicePromptSender;
use Qcloud\Sms\SmsStatusPuller;
use Qcloud\Sms\SmsMobileStatusPuller;
use Qcloud\Sms\VoiceFileUploader;
use Qcloud\Sms\FileVoiceSender;
use Qcloud\Sms\TtsVoiceSender;
/**
*公共短信类别
**/
class SmsController extends Controller{
public function _initialize(){
Vendor('qcloudsms.index');
// Vendor('qcloudsms.SmsSingleSender');
}
// 短信应用SDK AppID
public $appid = 1; //
// 短信应用SDK AppKey
public $appkey = "8";
// 需要发送短信的手机号码
public $phoneNumbers;
// 短信模板ID,需要在短信应用中申请
public $templateId; // NOTE: 这里的模板ID`7839`只是一个示例,真实的模板ID需要在短信控制台中申请
// 签名
public $smsSign; // NOTE: 这里的签名只是示例,请使用真实的已申请的签名,签名参数使用的是`签名内容`,而不是`签名ID`
//参数
public $params;
/**
* 发送短信
* 短信通知
* 指定模板单发短信
*/
public function send()
{
// 指定模板ID单发短信
try {
$ssender = new SmsSingleSender($this->appid,$this->appkey);
$params = $this->params;
$result = $ssender->sendWithParam('86',$this->phoneNumbers,$this->templateId, $this->params,
$sign = "", $extend = "", $ext = ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
$rsp = json_decode($result);
echo $result;
} catch (\Exception $e) {
echo var_dump($e);
}
if (($rsp && $rsp->errmsg == "OK") || ($rsp && $rsp->result == 0)) {
return array(
'code' => 0,
'msg' => '成功'
);
} else {
return array(
'code' => 1,
'msg' => $rsp->result.$rsp->errmsg
);
}
}