ThinkPHP3.2短信验证码 (创蓝253短信验证码)

创蓝253短信验证码

创蓝253短信验证码使用起来比阿里云的短信验证码简单。
下面放创蓝短信验证码的开发代码
我这里使用的框架是TP3,可能与你们的api放置位置不一样,

class ChuanglanSmsApi {
	//创蓝发送短信接口URL, 如无必要,该参数可不用修改
	const API_SEND_URL='http://sms.253.com/msg/HttpBatchSendSM';
	//创蓝短信余额查询接口URL, 如无必要,该参数可不用修改
	const API_BALANCE_QUERY_URL='http://sms.253.com/msg/QueryBalance';
	const API_ACCOUNT='';//创蓝账号 替换成你自己的账号
	const API_PASSWORD='';//创蓝密码 替换成你自己的密码
	/**
	 * 发送短信
	 * @param string $mobile 		手机号码
	 * @param string $msg 			短信内容
	 * @param string $needstatus 	是否需要状态报告
	 */
	public function sendSMS( $mobile, $msg, $needstatus = 'false') {
		//创蓝接口参数
		$postArr = array (
				          'account' => self::API_ACCOUNT,
				          'pswd' => self::API_PASSWORD,
				          'msg' => $msg,
				          'mobile' => $mobile,
				          'needstatus' => $needstatus
                     );
		$result = $this->curlPost( self::API_SEND_URL , $postArr);
		return $result;
	}
	/**
	 * 查询额度
	 *  查询地址
	 */
	public function queryBalance() {
		//查询参数
		$postArr = array ( 
		          'account' => self::API_ACCOUNT,
		          'pswd' => self::API_PASSWORD,
		);
		$result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
		return $result;
	}
	/**
	 * 处理返回值
	 */
	public function execResult($result){
		$result=preg_split("/[,\r\n]/",$result);
		return $result;
	}
	/**
	 * 通过CURL发送HTTP请求
	 * @param string $url  //请求URL
	 * @param array $postFields //请求参数 
	 * @return mixed
	 */
	private function curlPost($url,$postFields){
		$postFields = http_build_query($postFields);
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
		$result = curl_exec ( $ch );
		curl_close ( $ch );
		return $result;
	}
	//魔术获取
	public function __get($name){
		return $this->$name;
	}
	//魔术设置
	public function __set($name,$value){
		$this->$name=$value;
	}
}

做好配置后,就可以在你的控制器中直接使用了
我这块的演示代码是用Thinkphp3.2

public function getVerifyCode(){
	$vcinfo=$M->where($map)->find();
    vendor('ChuanglanSMS.ChuanglanSMS');
    $result=$sms->sendSMS($phonenumber,'【短息签名】短信内容以及验证码);
    $res = $sms->execResult($result);
    if ($res[1]==0) {                
    	echo '验证码发送成功!';
    }else{
        echo '验证码发送失败!';
	} 
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过使用第三方短信平台的 API 来发送短信验证码。以下是一个使用阿里云短信服务的示例代码: ```php // 引入阿里云 SDK require_once __DIR__ . '/aliyun-php-sdk-core/Config.php'; use Aliyun\Core\Config; use Aliyun\Core\DefaultAcsClient; use Aliyun\Core\Profile\DefaultProfile; use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest; // 配置信息 Config::load(); $accessKeyId = 'your_access_key_id'; $accessKeySecret = 'your_access_key_secret'; $signName = 'your_sign_name'; // 签名名称 $templateCode = 'your_template_code'; // 模板CODE // 发送短信验证码 $mobile = 'your_mobile_number'; $code = rand(100000, 999999); // 生成随机验证码 $paramString = '{"code":"' . $code . '"}'; // 短信模板参数 $profile = DefaultProfile::getProfile('cn-hangzhou', $accessKeyId, $accessKeySecret); $acsClient = new DefaultAcsClient($profile); $request = new SendSmsRequest(); $request->setPhoneNumbers($mobile); $request->setSignName($signName); $request->setTemplateCode($templateCode); $request->setTemplateParam($paramString); $response = $acsClient->getAcsResponse($request); // 处理响应结果 if ($response->Code == 'OK') { // 发送成功,将验证码存储到 session 或缓存中 session('sms_code', $code); echo '验证码已发送,请注意查收'; } else { echo '发送失败:' . $response->Message; } ``` 需要注意的是,上述代码中的 `your_access_key_id`、`your_access_key_secret`、`your_sign_name`、`your_template_code` 和 `your_mobile_number` 都需要根据实际情况进行替换。同时,由于阿里云 SDK 并不包含在 ThinkPHP 中,需要手动下载并添加到项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值