微信小程序生成太阳码

https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=access_token

必须通过POST提交

而且参数必须是JSON的格式

<?php

/**
* curl请求数据
*
* @param string $url 请求地址
* @param array $param 请求参数
* @param string $contentType 请求参数格式(json)
* @return boolean|mixed
*/
function https_request($url = '', $param = [], $contentType = ''){
$ch = curl_init(); // 请求地址
curl_setopt($ch, CURLOPT_URL, $url); // 请求参数类型
$param = $contentType == 'json' ? json_encode($param) : $param; // 关闭https验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // post提交
if($param){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
} // 返回的数据是否自动显示
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行并接收响应结果
$output = curl_exec($ch); // 关闭curl
curl_close($ch); return $output !== false ? $output : false;
} $access_token="10_X1w4ypXMdfliiv4orym7n7Ur8UMV3LsPAyyBng-DOjcZfAW1mlfKb1BAvBQuBIMUvk_Bq2lv3E2TI8QLvTrnmy1TBxoDeXu_JvR_sobPBkUimmA-aNasktu-J6UWLCgAAAFUL";
$request_url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
$request_data=array(
'scene'=>'abcdef',
'page'=>'pages/home/index',
'width'=>'690'
); print_r(https_request($request_url,$request_data,'json'));

这里有几点需要注意,page参数中的值一定要是小程序中存在的。

这里的access_token是用小程序的Appid和AppSecret生成的。之前还傻乎乎的去开启公众号的APPSecret。

再一个,这里返回的数据,不是JSON数据,而是二维码图片数据。

如何通过postman操作呢?

万能的POSTMAN啊。

{"page":"pages/home/index","scene":"abcdef","width":690}

封装

封装接口

// 获取太阳码
public function get_xcx_code() {
$uid = $_POST['uid'];
if (!$uid) {
$this->json->setErr('10001', '缺少参数');
$this->json->Send();
} // 获取用户信息
$user_model = M('user');
$user_info = $user_model->where(['id'=>$uid])->find();
if (!$user_info) {
$this->json->setErr('10002', '用户不存在');
$this->json->Send();
} $scene = $user_info['invite_code']; vendor('Func.Http');
$request_data = [
'scene' => $scene,
'page' => "pages/home/index",
'width' => 690
]; $request_url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->get_access_token();
$result = Http::doPostJson($request_url,$request_data);
header('Content-Type: image/jpeg; charset=UTF-8');
echo $result;exit;
}

处理成base64

// 获取太阳码
public function get_xcx_code() {
$uid = $_POST['uid'];
if (!$uid) {
$this->json->setErr('10001', '缺少参数');
$this->json->Send();
} // 获取用户信息
$user_model = M('user');
$user_info = $user_model->where(['id'=>$uid])->find();
if (!$user_info) {
$this->json->setErr('10002', '用户不存在');
$this->json->Send();
}
$scene = $user_info['invite_code'];
vendor('Func.Http');
$request_data = [
'scene' => $scene,
'page' => "pages/home/index",
// 'width' => 690
];
$request_url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->get_access_token();
$result = Http::doPostJson($request_url,$request_data);
// ob_clean();
// header('Content-Type: image/png; charset=UTF-8');
// echo $result;exit;
$this->json->setErr(0, '获取成功');
$this->json->setAttr('data',base64_encode($result));
$this->json->Send();
}

封装post请求

// 通过POST方式发送json数据
static public function doPostJson($url = '', $param = [] ,$contentType = 'json') {
$ch = curl_init();
// 请求地址
curl_setopt($ch, CURLOPT_URL, $url);
// 请求参数类型
$param = $contentType == 'json' ? json_encode($param) : http_build_query($param);
// 关闭https验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// post提交
if($param){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
}
// 返回的数据是否自动显示
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 执行并接收响应结果
$output = curl_exec($ch);
// 关闭curl
curl_close($ch);
return $output !== false ? $output : false;
}

继续升级

 // 获取太阳码
public function get_xcx_code() {
$uid = $_POST['uid'];
if (!$uid) {
$this->json->setErr('10001', '缺少参数');
$this->json->Send();
} // 查看是否已存储到数据库
$user_suncode_model = M('user_suncode');
$user_suncode_info = $user_suncode_model->where(['uid'=>$uid])->find();
if ($user_suncode_info) {
$this->json->setErr(0, '获取成功');
$this->json->setAttr('data',$user_suncode_info['code_imgurl']);
$this->json->Send();
} // 获取用户信息
$user_model = M('user');
$user_info = $user_model->where(['id'=>$uid])->find();
if (!$user_info) {
$this->json->setErr('10002', '用户不存在');
$this->json->Send();
} $scene = $user_info['invite_code'];
vendor('Func.Http');
$request_data = [
'scene' => $scene,
'page' => "pages/home/index",
];
$request_url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->get_access_token();
$result = Http::doPostJson($request_url,$request_data); // 存入cdn
$cdn_result = $this->upload_cdn($result,'suncode'); if ($cdn_result['errno'] == 0) {
// 存入数据库
$add_data = [
'uid' => $uid,
'code_imgurl' => $cdn_result['save_name'],
'addtime' => time()
]; $user_suncode_model = M('user_suncode');
$user_suncode_model->add($add_data); $this->json->setErr(0, '获取成功');
$this->json->setAttr('data',$cdn_result['save_name']);
$this->json->Send();
} else {
$this->json->setErr(10099, '获取失败');
$this->json->Send();
}
}

cdn函数

public function upload_cdn($img_result,$folders="common"){
$date_folders = date('Ymd',time());
$savePath = "site_upload/".$folders.'/'.$date_folders.'/';// 设置附件上传目录
if (!is_dir($savePath)){
@mkdir('./'.$savePath, 0777,true);
} $file_name = time().'_'.mt_rand().".png";
$upload_flag = file_put_contents($savePath.$file_name,$img_result); if($upload_flag){
vendor('Qiniu.Qiniu');
$qiniu = new Qiniu(); $img = C('SF_HOST'). $savePath . $file_name; $ext = pathinfo($img, PATHINFO_EXTENSION);
$name = time() . mt_rand() . '.' . $ext;
$s = $qiniu->up($img, $name, C('QINIU.BUCKET')); if($s){
@unlink('./'.$savePath . $file_name);
$res['errno']='0';
$res['errmsg']='ok';
$res['save_name'] = C('CDN.URI') . $name;
}else{
@unlink('./' .$savePath . $file_name);
$res['errno']='10001';
$res['errmsg']='上传cdn失败';
}
}else{
$res['errno']='10002';
$res['errmsg']='上传图片失败';
} return $res; }

微信小程序生成太阳码的更多相关文章

  1. 复用微信小程序源码包后仍然有原小程序的版本管理怎么处理

    前言: 复用微信小程序源码包后,重新创建项目导入源码包,会发现开发者工具版本管理中仍然有原来小程序的版本,这样就不太好了.毕竟是一个新的小程序,需要有新的版本控制的.那么这个问题怎么处理呢? 解决方案 ...

  2. 反编译获取线上任何微信小程序源码(转)

    看到人家上线的小程序的效果,纯靠推测,部分效果在绞尽脑汁后能做出大致的实现,但是有些细节,费劲全力都没能做出来.很想一窥源码?查看究竟?看看大厂的前端大神们是如何规避了小程序的各种奇葩的坑?那么赶紧来 ...

  3. 【转】精选十二款餐饮、快递、票务行业微信小程序源码demo推荐

    微信小程序的初衷是为了线下实体业服务的,必须有实体相结合才能显示小程序的魅力.个人认为微信小程序对于餐饮业和快递业这样业务比较单一的行业比较有市场,故整理推荐12款餐饮业和快递业微信小程序源码demo ...

  4. 【转】反编译获取任何微信小程序源码(完)

    一.前言最近在学习微信小程序开发,半个月学习下来,很想实战一下踩踩坑,于是就仿写了一个阿里妈妈淘宝客小程序的前端实现,过程一言难尽,差不多两周时间过去了,发现小程序的坑远比想象的要多的多!!在实际练手 ...

  5. 转载:微信小程序源码提取反编译

    转载来源:www.51xuediannao.com/xiaochengxu/019c08cc.html 一.前言 微信小程序源码提取反编译,听起来很屌,其实还是简单的,基本是傻瓜式操作.要想拿到微信小 ...

  6. 如何找回微信小程序源码?2020年微信小程序反编译最新教程 小宇子李

    前言:在网上看了找回微信小程序源码很多教程,都没法正常使用.微信版本升级后,会遇到各种报错, 以及无法获取到wxss的问题.查阅各种资料,最终解决,于是贴上完整的微信小程序反编译方案与教程. 本文章仅 ...

  7. 别错过了,130+个微信小程序源码 “限时分享“

    ​里面有130+款微信小程序源码和效果图,我只放了其中几款小程序的截图,具体请看下方图片 ​ ​ ​ ​ ​ ​ ​ ​ 仿网易云音乐小程序源码 链接:https://pan.baidu.com/s/ ...

  8. 微信小程序生成带参数的二维码 小程序二维码

    我是用php写的 先按照要求生成accesstoken $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=clien ...

  9. 微信小程序生成二维码

    微信小程序客户端生成二维码的方法, 请参考这里: https://github.com/demi520/wxapp-qrcode  代码片段如下: const QR = require(". ...

随机推荐

  1. 包嗅探和包回放 —tcpdump、tcpreplay--重放攻击

    攻击方式:tcpdump 进行嗅探,获取报文消息:然后用tcpreplay回放攻击 arp欺骗可以使用 arpspoof kali linux有这三个工具 转载地址https://www.cnblog ...

  2. git下载别人的代码

    1. 打开别人github上的源码地址,点击Clone or download 2. 拷贝链接 3. 通过git clone URL来下载 此外,还可以通过pwd来查看当前目录的路径,一般都是下载到当 ...

  3. 关于 SimpleDateFormat 的非线程安全问题及其解决方案

    一直以来都是直接用SimpleDateFormat开发的,没想着考虑线程安全的问题,特记录下来(摘抄的): 1.问题: 先来看一段可能引起错误的代码: package test.date; impor ...

  4. New Concept English three (30)

    27W/m 32 words the death of ghost For years, villagers believed that Endley Farm was hunted. The far ...

  5. android源码下载以及编译自己的ROM

    android源码下载以及编译自己的ROM 最近发现kernel.org被墙了,为什么这种网站也能被墙了? 要想下载源码的话,只能绕过去了.下面是我从网上搜索到的一些下载方法: =========== ...

  6. Android 进阶13:几种进程通信方式的对比总结

    不花时间打基础,你将会花更多时间解决那些不必要的问题. 读完本文你将了解: RPC 是什么 IDL 是什么 IPC 是什么 Android 几种进程通信方式 如何选择这几种通信方式 Thanks RP ...

  7. Leetcode 1006. Clumsy Factorial

    class Solution(object): def clumsy(self, N): """ :type N: int :rtype: int "" ...

  8. what is out of band mode.

    Most of the steps are the same, except instead of sending an URL as the oauth_callback to request_to ...

  9. 演示使用Metasploit入侵Windows

    我使用Kali Linux的IP地址是192.168.0.112:在同一局域网内有一台运行Windows XP(192.168.0.108)的测试电脑. 本文演示怎么使用Metasploit入侵win ...

  10. 瀑布流下滑 发送ajax

    <!DOCTYPE=html>      <html>      <head>      <meta charset="UTF-8"> ...