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. maven install中依赖关系打包failed

    maven 中maven dependencies中依赖出现了项目,无法打包 ,出现的错误如图.说明:依赖的项目为project-dao  打包的项目为project-service  都在proje ...

  2. Oracle自我补充之trunc()函数使用介绍

    oracle trunc函数使用介绍 核心提示:oracle trunc函数使用介绍 1.TRUNC(for dates) TRUNC函数为指定元素而截去的日期值. 其具体的语法格式如下: TRUNC ...

  3. 大数据-07-Spark之流数据

    摘自 http://dblab.xmu.edu.cn/blog/1084-2/ 简介 DStream是Spark Streaming的编程模型,DStream的操作包括输入.转换和输出. Spark ...

  4. golang相关网摘

    1.golang开发50个坑 http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/index.html#mli ...

  5. Unity等待写入数据

    EasyTouch中QuickGesture的用法 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Qu ...

  6. Unity 3D委托entrust

    Unity 3D委托的多种用法 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...

  7. 测试那些事儿—BUG

    一.作为测试人员,你应该这样报BUG: 不要对程序员说,你的代码有BUG. 他的第一反应是:1.你的环境有问题吧:2.你踏马到底会不会用? 如果你委婉的说:你这个程序和预期的不一样,你看看是不是我的方 ...

  8. xdoj 1330---异或(找规律)

    我是打表找的规律 233 样例什么作用都没有 只会迷惑作用... 1330: 天才琪露诺的完美算数教室 时间限制: 1 Sec  内存限制: 128 MB  Special Judge提交: 37   ...

  9. 2017-2018-1 20155208 课堂测试(ch06)(补做)

    2017-2018-1 20155208 课堂测试(ch06)(补做) 1.( 多选题 | 1 分) 下面说法正确的是(ABC) A . 存储层次结构中最小的缓存是寄存器 B . 存储层次结构的中心思 ...

  10. log4j.properties与db.properties

    log4j.properties与db.properties db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql:///mybatis?useUnico ...