【1】踩了一上午的坑 接口配置一直不成功,直到方法换成index
//验证
public function index()
{
echo input('echostr');
}
这儿是一个神奇的地方,之前写的代码不成功,换成这个之久就成功了 ,比较神奇
之前的代码
//验证
public function checkSignature()
{
$signature = input('signature');
$timestamp = input('timestamp');
$nonce = input('nonce');
$token = 'cJ8CQE5ZQ5QgjOV5g8C9xUuOqoZA4Nqj';
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
开始开发模版消息
//【会员充值】发送模版消息
public function set_msg($out_trade_no,$total_fee){
$uri ='https://api.weixin.qq.com/cgi-bin/message/template/send';
$access_token = $this->getAccess_token();;
$uri = $uri.'?access_token='.$access_token;
$data= array('touser'=>'oXQ9Gt3j79F6m3kSKZteEiICkQLw', //发给谁
'template_id'=>'wRaL7SsmjNAWocMjbo-bGo4Pdmy5vNZpTEbwSz19M5E', //消息模板id
'url'=>'#', //这个是你发送了模板消息之后,当用户点击时跳转的连接
'topcolor'=>"#FF0000", //颜色
'miniprogram' => '',
'data'=>array(
'first'=>array(
'value'=>'会员充值',
'color'=>'#173177'
),
'keyword1'=>array(
'value'=>date('Y-m-d H:i:s',time()),
'color'=>'#173177'
),
'keyword2'=>array(
'value'=>$total_fee,
'color'=>'#173177'
),
'keyword3'=>array(
'value'=>$out_trade_no,
'color'=>'#173177'
),
'remark'=>array(
'value'=>'您的充值金额已到账,请注意查看',
'color'=>'#173177'
)
)
);
$res_data = $this->sendUrl($uri,$data);
$res_data = json_decode($res_data, true);
if ($res_data['errcode'] != 0) {
return false;
}
return true;
}
//获取Access_token
public function getAccess_token(){
$appid='wxdffdacdd4ee5795b';
$appsecret='43306c632356d60d2c407a4108dad57d';
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret.'';
$data=$this->sendUrl($url);
$res=json_decode($data,true);
return $res['access_token'];
}
//发送请求
function sendUrl($url = '', $param = [] ,$contentType = 'json'){
$ch = curl_init();
// 请求地址
curl_setopt($ch, CURLOPT_URL, $url);
// 请求参数类型
$param = $contentType == 'json' ? urldecode(json_encode($param)) : http_build_query($param);
// 关闭https验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// post提交
if($param){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
}
// 返回的数据是否自动显示
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 执行并接收响应结果
$output = curl_exec($ch);
// 关闭curl
curl_close($ch);
return $output !== false ? $output : false;
}