-
先申请商户和应用配置文件:
-
源码:
HTML部分
<img id="pscover" src="__PUBLIC__/Home/images/z_70.jpg" alt="" /> <input type="file" name="scover" id="scover" /> <img id="pqr" src="__PUBLIC__/Home/images/z_70.jpg" alt="" /> <input type="file" name="qr" id="qr"/>
JS部分(注意引入jQ)
window.onload = function(){
var arr = new Array();
arr["img[id='pscover']"] = "input[name='scover']";
arr["img[id='pqr']"] = "input[name='qr']";
for(var i in arr){
void function(i){
$(arr[i]).on("change",function(){
var pic = this.files[0];
pic_preview(pic,i);
})
}(i)
}
function pic_preview(pic,obj){
var f = new FileReader();
f.readAsDataURL(pic);
f.onload = function (e){
$(obj).attr("src",this.result).show();
}
}
}
PHP 7 可以使用一个 use 从同一个 namespace 中导入类、函数和常量:
// PHP 7 之前版本需要使用多次 use
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;
// PHP 7+ 之后版本可以使用一个 use 导入同一个 namespace 的类
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
html代码:
<input class="inpt-hm" type="text" id="mobile" name="mobile" placeholder="请输入手机号码"> <input class="yzma-put" type="text" placeholder="请输入验证码"> <input id="btnSendCode" type="button" id="btn" value="发送验证码" onclick="sendMessage()">
2. js代码:
var InterValObj; //timer变量,控制时间
var count = 60; //间隔函数,1秒执行
var curCount;//当前剩余秒数
function sendMessage() {
curCount = count;
var mobile = $("input.inpt-hm").val();
if(mobile.length != 11 || mobile.substr(0,1)!=1){
alert("手机格式不正确!");
$("input.inpt-hm").select();
return false;
}else{
//设置button效果,开始计时
$("#btnSendCode").attr("disabled", "true");
$("#btnSendCode").val(curCount + "秒后重新发送");
//启动计时器,1秒执行一次
InterValObj = window.setInterval(SetRemainTime, 1000);
$.ajax({
url:"/SendSMS.php",
data:{
mobile:mobile
},
dataType:'json',
type:'post',
success:function (data) {
console.table(data);
if(data['success'] == "false"){
alert("系统错误");
}
}
});
}
}
//timer处理函数
function SetRemainTime() {
if (curCount == 0) {
window.clearInterval(InterValObj);//停止计时器
$("#btnSendCode").removeAttr("disabled");//启用按钮
$("#btnSendCode").val("重新发送验证码");
}
else {
curCount--;
$("#btnSendCode").val(curCount + "秒后重新发送");
}
}
3. SendSMS.php
<?php
/**
* 启瑞云短信接口Demo(utf-8)
*/
class SendDemo
{
const SENDURL = 'http://api.qirui.com:7891/mt';
private $apiKey;
private $apiSecret;
/**
* 构造方法
* @param string $apiKey 接口账号
* @param string $apiSecret 接口密码
*/
public function __construct($apiKey, $apiSecret)
{
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
}
/**
* 短信发送
* @param string $phone 手机号码
* @param string $content 短信内容
* @param integer $isreport 是否需要状态报告
* @return void
*/
public function send($phone, $content, $isreport = 1)
{
$requestData = array(
'un' => $this->apiKey,
'pw' => $this->apiSecret,
'sm' => $content,
'da' => $phone,
'rd' => $isreport,
'dc' => 15,
'rf' => 2,
'tf' => 3,
);
$url = self::SENDURL . '?' . http_build_query($requestData);
return $this->request($url);
}
/**
* 请求发送
* @return string 返回发送状态
*/
private function request($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
//end
//实例化类,测试发送
//接口账号
$apiKey = '1777270010';
//接口密码
$apiSecret = 'bcd9e627095d553da10d';
//接受短信的手机号码
$phone = empty($_POST["mobile"])?"15872024172":$_POST["mobile"];
//短信内容(【签名】+短信内容),系统提供的测试签名和内容,如需要发送自己的短信内容请在启瑞云平台申请签名和模板
$yzm = rand(1000,9999);
$content = '【首房在线】'.$phone.'您好,您的验证码是:'.$yzm.",请在3分钟内完成验证。";
session_start();
$_SESSION['yzm'] = $yzm;
$sms = new SendDemo($apiKey, $apiSecret);
$result = $sms->send($phone, $content);
print_r($result );
在PHP 4, PHP 5, PHP 7中,call_user_func — 把第一个参数作为回调函数调用。
mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] )
第一个参数 callback 是被调用的回调函数,其余参数是回调函数的参数,传入call_user_func()的参数不能为引用传递。
Example #1 call_user_func() 的参考例子
<?php
error_reporting(E_ALL);
function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func('increment', $a);
echo $a."\n";
//You can use this instead before PHP 5.3
call_user_func_array('increment', array(&$a));
echo $a."\n";
?>
以上例程会输出:
0 1
近期评论