码迷,mamicode.com
首页 > 微信 > 详细

php小程序生成二维码

时间:2018-08-30 18:26:28      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:str   encode   turn   tran   bin   pat   object   mic   etop   

<?php

getwxacode();

//生成二维码
function getwxacode(){
$url = "https://api.weixin.qq.com/wxa/getwxacode?";
$url .= "access_token=" . getToken();
$postdata = [
"path" => "pages/index/index?clientId=3",
"width" => 430,
];

$res = curl_post($url,json_encode($postdata),$options=array());
$img = ‘./img/‘.time().‘.jpg‘;
$r = file_put_contents($img,$res);
echo "<img src=‘".$img."‘>";

}


//发送获取token请求,获取token(2小时)
function getToken() {
$url = getTokenUrlStr();
$res = curl_post($url,$postdata=‘‘,$options=array());

$data = json_decode($res,JSON_FORCE_OBJECT);
return $data[‘access_token‘];
}

//获取token的url参数拼接
function getTokenUrlStr()
{
$getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?"; //获取token的url
$WXappid = "wx0399845015067c51"; //APPID
$WXsecret = "7d7205f85b79805006ecfa2121ef4272"; //secret
$str = $getTokenUrl;
$str .= "grant_type=client_credential&";
$str .= "appid=" . $WXappid . "&";
$str .= "secret=" . $WXsecret;

return $str;
}

function curl_post($url=‘‘,$postdata=‘‘,$options=array()){
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if(!empty($options)){
curl_setopt_array($ch, $options);
}
$data=curl_exec($ch);
curl_close($ch);
return $data;
}

function curl_get($url=‘‘,$options=array()){
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,5);
if(!empty($options)){
curl_setopt_array($ch,$options);
}
$data=curl_exec($ch);
curl_close($ch);
return $data;
}

php小程序生成二维码

标签:str   encode   turn   tran   bin   pat   object   mic   etop   

原文地址:https://www.cnblogs.com/cqlb/p/9561376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!