微信JS API PHP类
CURL操作类:
<?php
namespace app\common;
class curl{
public static function wxcurl($getUrl){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $getUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
$response = json_decode($data,true);
return $response;
}
}
PHP类
直接写入的是缓存,如果你本地不支持redis缓存 需要修改存储方式
<?php
namespace app\common;
use app\common\Curl;
/**
* @property secret
*/
class Wechat{
private $appid;
private $secret;
private $url;
private $urlaccesstoken="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
private $urljsapi_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=";
function __construct($appid,$secret,$url){
$this->appid=$appid;
$this->secret =$secret;
$this->url = $url;
}
private function getToken(){
$access_token = json_decode(\Yii::$app->redis->get('wechat_access_token'),true);
$effective = false;
if(!$access_token){
$effective=true;
}else{
//验证时间
if(time() -$access_token['times'] > 7200){
$effective=true;
}
}
if($effective){
$oauthUrl = $this->urlaccesstoken."&appid=".$this->appid."&secret=".$this->secret;
$codeinfo = curl::wxcurl($oauthUrl);
$access_token = ["times"=>time(),"access_token"=>$codeinfo['access_token']];
\Yii::$app->redis->set('wechat_access_token',json_encode($access_token,true));
}
return $access_token['access_token'];
}
private function getTicket(){
$jsapi_ticket =json_decode(\Yii::$app->redis->get('wechat_jsapi_ticket'),true);
$effective = false;
if(!$jsapi_ticket){
$effective=true;
}else{
//验证时间
if(time() -$jsapi_ticket['times'] > 7200){
$effective=true;
}
}
if($effective){
$access_token = $this->getToken();
$oauthUrl = $this->urljsapi_ticket.$access_token;
$codeinfo = curl::wxcurl($oauthUrl);
$jsapi_ticket = ["times"=>time(),"ticket"=>$codeinfo['ticket']];
\Yii::$app->redis->set('wechat_access_ticket',json_encode($jsapi_ticket,true));
}
return $jsapi_ticket['ticket'];
}
private function nonceStr($length=32){
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
private function signature($Parameters){
//拼接参数
$arg = "";
foreach($Parameters as $key => $val)
{
$arg.=$key."=".$val."&";
}
//去掉最后一个&字符
$arg = trim($arg,'&');
if(get_magic_quotes_gpc())
{
$arg = stripslashes($arg);
}
return sha1($arg);
}
private function getAppid(){
return $this->appid;
}
function getWechatJsConfig(){
$time = time();
$Parameters = [
"jsapi_ticket"=>$this->getTicket(),
"noncestr"=>$this->nonceStr(),
"timestamp"=>"$time",
"url"=>$this->url,
];
$return['appId'] = $this->getAppid();
$return['timestamp'] = $Parameters['timestamp'];
$return['nonceStr'] = $Parameters['noncestr'];
$return['signature'] = $this->signature($Parameters);
return $return;
}
}
返回格式:
appId:"xxxxxxxxx",
nonceStr:"xxxxxxxxxxx",
signature:"xxxxxxxxxx",
timestamp:"1522083380",
JS页面
wx.config({
debug: true,
appId:res.appId,
timestamp:res.timestamp,
nonceStr:res.nonceStr,
signature:res.signature,
jsApiList: [
'chooseImage',//拍照或从手机相册中选图接口
'onMenuShareTimeline',
'previewImage',//预览图片接口
'uploadImage',//上传图片接口
'downloadImage'//下载图片接口
]});
微信JS API PHP类的更多相关文章
- 微信 JS API 支付教程
最近一个项目中用到了微信开发,之前没有做过支付相关的东西,算是拿这个来练练手,刚开始接触支付时候很懵逼,加上微信支付开发文档本来就讲得不清楚,我是彻底蒙圈了,参考了很多代码之后,算是有一点思路了. 用 ...
- 微信 js api[转]
rainbow661314 微信api /**! * 微信内置浏览器的Javascript API,功能包括: * * 1.分享到微信朋友圈 * 2.分享给微信好友 * 3.分享到腾讯微博 * 4.新 ...
- 当VUE 遇到微信js api
先哭一会儿... history hash 1.如果使用history 方式,需要使用window.localtion.href="路径" 2.然后使用this.$router.p ...
- 微信支付开发(1) JS API支付
关键字:微信支付 微信支付v3 jsapi支付 统一支付 Native支付 prepay_id 作者:方倍工作室原文: http://www.cnblogs.com/txw1958/p/wxpayv3 ...
- 微信支付开发(1) JS API支付V3版(转)
http://www.cnblogs.com/txw1958/p/wxpayv3-jsapi.html 本文介绍微信支付下的jsapi实现流程 前言 微信支付现在分为v2版和v3版,2014年9月10 ...
- 微信支付.NET版开发总结(JS API),好多坑,适当精简
前2天,做一个手机网页的微信支付的项目,费了好些周折,记录一下.接下来,按照开发步骤,细数一下,我遇到的那些坑. [坑1]官方邮件中下载的demo只有PHP版本,其他版本没有给链接.可能让人误以为只有 ...
- 微信支付开发(3) JS API支付
由于微信支付接口更新,本文档已过期,请查看新版微信支付教程.地址 http://www.cnblogs.com/txw1958/category/624506.html 本文介绍如何使用JS API支 ...
- 微信支付.NET版开发总结(JS API),好多坑,适当精简。
前2天,做一个手机网页的微信支付的项目,费了好些周折,记录一下.接下来,按照开发步骤,细数一下,我遇到的那些坑. [坑1]官方邮件中下载的demo只有PHP版本,其他版本没有给链接.可能让人误以为只有 ...
- 微信支付v2开发(3) JS API支付
本文介绍如何使用JS API支付接口完成微信支付. 一.JS API支付接口(getBrandWCPayRequest) 微信JS API只能在微信内置浏览器中使用,其他浏览器调用无效.微信提供get ...
随机推荐
- 20155208徐子涵 2016-2017-2 《Java程序设计》第9周学习总结
20155208徐子涵 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 16.1 JDBC入门 撰写应用程序是利用通信协议对数据库进行指 ...
- Sublime Text3:插件+快捷键+环境变量设置+C/C++编译环境
环境变量配置 如果电脑里之前下载过Codeblocks或者Dev,找到文件根目录,打开MinGw(或者MinGw64),点开bin目录,将bin文件夹的目录复制下来(以Codeblocks为例,将文件 ...
- Socket远程桌面
自建Socket转发,使用远程桌面(mstsc)连接家中电脑 网络结构图如下: 开题先放图,一切全靠编哈哈. 进入正题! 如图所示,我们需要一个公网服务器,利用公网服务器将内网的数据进行转发,从而 ...
- java-内部类访问特点-私有成员内部类-静态成员内部类-局部内部类访问局部变量
1.内部类访问特点: - 内部类可以直接访问外部类的成员,包括私有. - 外部类要访问内部类的成员,必须创建对象. - 外部类名.内部类名 对象名 = 外部类对象.内部类对象: - 例: class ...
- 一次scrapy成功停止的信息
2017-11-05 18:52:42 [scrapy.core.engine] INFO: Closing spider (finished)2017-11-05 18:52:42 [scrapy. ...
- C++学习(十三)(C语言部分)之 练习
打印图像 要用循环 空心或者实心都可以1.矩形 菱形 三角形 梯形 六边形2.打印倒三角形的99乘法表 具体测试代码如下: /* 1.打印图像 要用循环 空心或者实心都可以 矩形 菱形 三角形 梯形 ...
- ajax的原生调用
https://blog.csdn.net/jitianxia68/article/details/78393406
- python与系统做交互常用的模块和使用方法
1.使用os模块与系统做简单命令的交互 >>>import os >>>os.popen('pwd') <open file 'pwd', mode 'r' ...
- Oracle 11g direct path read 等待事件的理解
在Oracle 11g中,全表扫描可能使用direct path read方式,绕过buffer cache,这样的全表扫描就是物理读了. 在10g中,都是通过gc buffer来读的,所以不存在di ...
- div中 li宽度不固定 ie6和ie7不兼容不自动换行
我的li因为内容字数不一样,所以宽度不固定,给他float:left属性后,ie6和ie7不兼容,不自动换行!我给ul或者li: ul{white-space: nowrap} 属性还是不管用..最后 ...