微信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 ...
随机推荐
- 2.5 SeleniumBuilder辅助定位元素
前言对于用火狐浏览器的小伙伴们,你还在为定位元素而烦恼嘛?上古神器Selenium Builder来啦,哪里不会点哪里,妈妈再也不用担心我的定位元素问题啦!(但是也不是万能,基本上都能覆盖到) 2.5 ...
- pytorch实现style transfer
说是实现,其实并不是我自己实现的 亮出代码:https://github.com/yunjey/pytorch-tutorial/tree/master/tutorials/03-advanced/n ...
- poj-3666 【对dp子状态无后效性的理解】
题目链接 错解: #include <cstdio> #include <cstring> #include <algorithm> #include <io ...
- POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】
题目链接:传送门 题目大意: 求斐波那契数列第n项F(n). (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路: 用矩阵乘法加速递推. 算法竞赛进阶指南的模板: #includ ...
- Codeforces 766C:Mahmoud and a Message(DP)
题目链接:http://codeforces.com/problemset/problem/766/C 题意 有一个长度为n的字符串,第二行有26个数字,位置1~26对应为a~z的字母,数值表示该字母 ...
- HDU 1873 看病要排队 优先队列
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 如何运行简单的scrapy
1.建scrapy工程 scrapy startproject python123demo 2.在工程中写一个爬虫文件 cd python123demo scrapy genspider demo p ...
- 20165313 《Java程序设计》第六周学习总结
教材学习总结 一点浅见,不足之处还请谅解. 正则表达式:正则表达式是一个String对象的字符序列,该字符序列中含有具有特殊意义的字符,这些特殊字符称作正则表达式的元字符. 链表:由若干个称作结点的对 ...
- Linux下安装搜狗拼音输入法
1.安装 下面命令即可完成安装: sudo apt-add-repository ppa:fcitx-team/nightly sudo apt-get update sudo apt-get ins ...
- MySql中的一些小坑
1. mysql启动时,若使用mysqld_safe的方式启动服务,需要使用mysqladmin shutdown的方式来停止服务. 若使用mysqld shutdown的方式停止服务,有可能会出现如 ...