微信小程序生成帶參數的二維碼 小程序二維碼


我是用php寫的 

先按照要求生成accesstoken

$tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
$getArr=array();
$tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));
$access_token=$tokenArr->access_token;

使用get的方法就可以獲取到accesstoken了 最好緩存起來   以免刷新太多   一天的請求數量不夠  微信好像規定是一天一萬次請求吧   

function send_post($url, $post_data,$method='POST') {
    $postdata = http_build_query($post_data);
    $options = array(
      'http' => array(
        'method' => $method, //or GET
        'header' => 'Content-type:application/x-www-form-urlencoded',
        'content' => $postdata,
        'timeout' => 15 * 60 // 超時時間(單位:s)
      )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
  }

然后就生成二維碼了

$path="pages/index?query=1";
$width=430;
$post_data='{"path":"'.$path.'","width":'.$width.'}';
$url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
$result=$this->api_notice_increment($url,$post_data);

 

調用下面的psot方法

function api_notice_increment($url, $data){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
// var_dump($tmpInfo);
// exit;
if (curl_errno($ch)) {
return false;
}else{
// var_dump($tmpInfo);
return $tmpInfo;
}
}

然后將圖片生成存到服務器里

file_put_contents($filepath, $result)

 

然后在前台輸出你在服務器存的圖片二維碼地址

 

 

還有不明白的  可以加微信溝通   我的微信號 qiufeng2983         我也是一個phper  現在開發微信小程序 主要做微信點餐小程序

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM