获取配置参数方法
function getWeixinConfigData($url){
$appId="公众号appid";
$secret="公众号secret";
//获取access_token
$token = S('WX_TOKEN');
if (!$token)
{
$getTokenUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appId.'&secret='.$corpsecret;
$getTokenData = curlRequestWithHeaderParam($getTokenUrl);
$getTokenData = json_decode($getTokenData,true);
if(!isset($getTokenData['access_token'])){
dump($getTokenData);
exit("can't get token ");
}
$token = $getTokenData['access_token'];
S('WX_TOKEN',$token,7100);
}
//获取jsapi_ticket
$ticket = S('WX_TICKET');
if (!$ticket)
{
$getTicketUrl = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
$getTicketData = curlRequestWithHeaderParam($getTicketUrl);
$getTicketData = json_decode($getTicketData,true);
if(!isset($getTicketData['ticket'])){
dump($getTicketData);
exit("can't get ticket ");
}
$ticket = $getTicketData['ticket'];
S('WX_TICKET',$ticket,7100);
}
$timestamp=time();
$nonceStr=time().rand(1000,9999);
$url='完整的h5url';
$str="jsapi_ticket=$ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature=sha1($str);
$arr=array();
$arr["appId"]=$appId;
$arr["timestamp"]=$timestamp;
$arr["nonceStr"]=$nonceStr;
$arr["signature"]=$signature;
return $arr;
}
curl
/**
* 发送请求,可携带header参数
* @param unknown $url 链接地址
* @param array $params 参数
* @param string $is_post 是否是post请求
* @param number $time_out 超时时间
* @param array $header 请求头信息
* @return mixed
*/
function curlRequestWithHeaderParam($url, $params = array(), $is_post = false, $time_out = 10, $header=array())
{
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置是否返回response header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
//当需要通过curl_getinfo来获取发出请求的header信息时,该选项需要设置为true
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $time_out);
curl_setopt($ch, CURLOPT_POST, $is_post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($is_post) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$response = curl_exec($ch);
$request_header = curl_getinfo($ch,CURLINFO_HEADER_OUT);
curl_close($ch);
return $response;
}
h5页面
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
$(document).ready(function(){
$.ajax({
type: "POST",
url: "{:U('Student/Activity/getWeixinParam')}",
async:false,
dataType: "json",
data: {
url:"{:U('Student/Activity/index')}"
},
success: function(data){
if(data!=null){
if(data!=null){
wx.config({
debug: false,
appId: data["appId"],
timestamp:data["timestamp"],
nonceStr:data["nonceStr"],
signature:data["signature"],
jsApiList : [ 'checkJsApi', 'scanQRCode','getLocation' ]
});
}
}
},
error:function(e){
// alert("error");
}
});
});
wx.getLocation({
success: function (res) {
/* alert(JSON.stringify(res));
res=$.parseJSON(JSON.stringify(res));
alert(res.latitude); */
$("#position").text(res.longitude+","+res.latitude);
data=res.longitude+"|"+res.latitude+"|"+$("#fanwei").val()+"|"+$("#position").text();
alert(data);
$.ajax({
type: "POST",
url: "接口地址",
async:false,
data: {
},
dataType: "json",
success: function(data){
if(data==-1){
mui.alert("开启定位签到失败!服务器返回:"+data);
return;
}else{
$("#ok").text("关闭定位签到");
$("#fanwei").attr("disabled",true);
$("#a_getPosition").hide();
mui.toast("定位签到已开启!");
}
}
});
},
cancel: function (res) {
alert( '用户拒绝授权获取地理位置');
}
});
wx.scanQRCode({
needResult : 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType : [ "qrCode", "barCode" ], // 可以指定扫二维码还是一维码,默认二者都有
success : function(res) {
var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
url="{:geturl('')}Student/My";
if(result.indexOf(url)!=0){
//return;
}
window.location=result;
}
});
标签:ch,url,微信,h5,res,curl,php,data,ticket
From: https://blog.csdn.net/weixin_51957364/article/details/137461821