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. js- caller、 callee

    caller 返回一个对函数的引用,该函数调用了当前函数.    functionName.caller     functionName对象 是所执行函数的名称.  说明        对于函数来说 ...

  2. threejs 空间位置转为屏幕像素xy位置

    function createVector(x, y, z, camera, width, height) { var p = new THREE.Vector3(x, y, z); var vect ...

  3. LBS推荐系统的设计方法

    https://www.csdn.net/article/2015-12-24/2826554 http://www.datayuan.cn/article/14797.htm https://my. ...

  4. Python urllib.quote

    转: 编码:urllib.quote(string[, safe]),除了三个符号“_.-”外,将所有符号编码,后面的参数safe是不编码的字符, 使用的时候如果不设置的话,会将斜杠,冒号,等号,问号 ...

  5. [LeetCode&Python] Problem 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  6. 苹果手机不兼容autoplay属性

    var audio=new Audio("music/music.mp3"); audio.preload="preload"; // 自动播放解决苹果不兼容a ...

  7. linux rpm yum 安装 软件

    rpm 安装: 1.rpm包的了解:  rpm  安装  升级  删除 rpm -ivh  ****.rpm   安装 rpm -Uvh  ****.rpm  升级 rpm -e name    删除 ...

  8. 树莓派ssh服务

    从官网下载的镜像更新raspberry pi 3 B,但默认是不支持SSH的,即不可外部通过SSH登陆到板子里. 解决办法很简单,在SD卡的根目录下创建一个"ssh"的文件夹即可.

  9. $trainClassLayer.find('input[name=data-item-checkbox]').eq(index).change();//激活第index+1那个checkbox

    ☆ $.each(data, function (index, org) { if (org.alreadySent) { $trainClassLayer.find('input[name=data ...

  10. debian 安装使用NTP

    编程之路刚刚开始,错误难免,希望大家能够指出. 领导要求,要4台机器时钟同步,上网查了查,主要看了看ptp和ntp,感觉ntp就够用,索性就直接上手ntp了. 以下内容纯属最基础的内容,只适合第一次接 ...