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类的更多相关文章

  1. 微信 JS API 支付教程

    最近一个项目中用到了微信开发,之前没有做过支付相关的东西,算是拿这个来练练手,刚开始接触支付时候很懵逼,加上微信支付开发文档本来就讲得不清楚,我是彻底蒙圈了,参考了很多代码之后,算是有一点思路了. 用 ...

  2. 微信 js api[转]

    rainbow661314 微信api /**! * 微信内置浏览器的Javascript API,功能包括: * * 1.分享到微信朋友圈 * 2.分享给微信好友 * 3.分享到腾讯微博 * 4.新 ...

  3. 当VUE 遇到微信js api

    先哭一会儿... history hash 1.如果使用history 方式,需要使用window.localtion.href="路径" 2.然后使用this.$router.p ...

  4. 微信支付开发(1) JS API支付

    关键字:微信支付 微信支付v3 jsapi支付 统一支付 Native支付 prepay_id 作者:方倍工作室原文: http://www.cnblogs.com/txw1958/p/wxpayv3 ...

  5. 微信支付开发(1) JS API支付V3版(转)

    http://www.cnblogs.com/txw1958/p/wxpayv3-jsapi.html 本文介绍微信支付下的jsapi实现流程 前言 微信支付现在分为v2版和v3版,2014年9月10 ...

  6. 微信支付.NET版开发总结(JS API),好多坑,适当精简

    前2天,做一个手机网页的微信支付的项目,费了好些周折,记录一下.接下来,按照开发步骤,细数一下,我遇到的那些坑. [坑1]官方邮件中下载的demo只有PHP版本,其他版本没有给链接.可能让人误以为只有 ...

  7. 微信支付开发(3) JS API支付

    由于微信支付接口更新,本文档已过期,请查看新版微信支付教程.地址 http://www.cnblogs.com/txw1958/category/624506.html 本文介绍如何使用JS API支 ...

  8. 微信支付.NET版开发总结(JS API),好多坑,适当精简。

    前2天,做一个手机网页的微信支付的项目,费了好些周折,记录一下.接下来,按照开发步骤,细数一下,我遇到的那些坑. [坑1]官方邮件中下载的demo只有PHP版本,其他版本没有给链接.可能让人误以为只有 ...

  9. 微信支付v2开发(3) JS API支付

    本文介绍如何使用JS API支付接口完成微信支付. 一.JS API支付接口(getBrandWCPayRequest) 微信JS API只能在微信内置浏览器中使用,其他浏览器调用无效.微信提供get ...

随机推荐

  1. m3u8编码视频webgl、threejs渲染视频纹理demo

    <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>fz-live< ...

  2. Python中的print、input函数以及Python中交换两个变量解析

    一.Python中的值交换操作 首先明确一点点,Python中的一切都是面向对象的,可以理解为Python的中一切都是对象. 我们知道Java也是面向对象的语言,但是在Java中定义一个值变量如下: ...

  3. hdu-2196 树形dp 求一个树中所有节点能到达的最远距离f[i] (其实也不难嘛!)

    #include <bits/stdc++.h> using namespace std; ; struct T { int to; int w; }; vector < vecto ...

  4. HTML+CSS之background

    第一个专题--background属性 今天写一下background属性,具体如下: 1.background-color:默认值:transparent,这是我们在做网页时,经常使用的属性,较为简 ...

  5. (16)模型层Models - ORM的使用

    需求:通过orm创建user表 先配置settings文件夹 连接数据库和配置数据库 Django的模块有两种 1.mysqlDB  django内置的模块,只能在python2.X版本下用 2.py ...

  6. Vue 1组件的使用

    在components新建一个vue组件: <template> <div> <h1>{{ msg }}</h1> </div> </ ...

  7. .NET本质论 方法

    方法和JIT编译 CLR只执行本机的机器代码.如果一个方法体由CIL组成,那么它就必须在调用之前被转换为本机的机器码(将MSIL编译为本机代码,运行库提供了两种方式.一种就是在安装与部署时的预编译(由 ...

  8. C#模拟键盘按键的三种方式实现

    1.System.Windows.Forms.SendKeys 组合键:Ctrl = ^ .Shift = + .Alt = % 模拟按键:A private void button1_Click(o ...

  9. 性能测试之mysql监控、优化

    我们在做性能测试的目的是什么,就是要测出一个系统的瓶颈在哪里,到底是哪里影响了我们系统的性能,找到问题,然后解决它.当然一个系统由很多东西一起组合到一起,应用程序.数据库.服务器.中中间件等等很多东西 ...

  10. 我发起了一个 .Net Core 平台上的 开源项目 ShadowDomain 用于 热更新

    大家好,  我发起了一个 .Net Core 平台上的 开源项目 ShadowDomain  用于 热更新 . 简单的说, 原理就是 类似 Asp.net 那样 让 当前 WebApp 运行在一个 A ...