微信小程序生成太阳码
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;
}