欢迎光临,了解微信小程序开发,就上易用通!

小程序生成参数二维码(PHP)

发布:2018-01-24 18:24浏览: 来源:网络 作者:tianshu

官方获取小程序页面二维码API 小程序生成参数二维码(PHP)(图1) 


由于小程序参数二维码API提供的帮助有限,以下是我对该功能的一些理解


我主要是通过thinkphp后台接口实现,代码如下:
  1、先获取ACCESS_TOKEN:
  1. $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
  2.         $getArr=array();
  3.         $tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));
  4.  
  5.         $access_token=$tokenArr->access_token;
send_post:
  1. function send_post($url, $post_data,$method='POST') {
  2.         $postdata = http_build_query($post_data);
  3.         $options = array(
  4.             'http' => array(
  5.                 'method' => $method, //or GET
  6.                 'header' => 'Content-type:application/x-www-form-urlencoded',
  7.                 'content' => $postdata,
  8.                 'timeout' => 15 * 60 // 超时时间(单位:s)
  9.             )
  10.         );
  11.         $context = stream_context_create($options);
  12.         $result = file_get_contents($url, false, $context);
  13.  
  14.  
  15.         return $result;
  16.     }


2、生成二维码:
  1. $path="pages/index?query=1";
  2.         $width=430;
  3.         $post_data='{"path":"'.$path.'","width":'.$width.'}';
  4.  
  5.         $url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
  6.  
  7.  
  8.         $result=$this->api_notice_increment($url,$post_data);
api_notice_increment:
  1. function api_notice_increment($url, $data){
  2.         $ch = curl_init();
  3.         $header = "Accept-Charset: utf-8";
  4.         curl_setopt($ch, CURLOPT_URL, $url);
  5.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  6.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  7.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  8.         curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  9.         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  10.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  11.         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  12.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  13.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14.         $tmpInfo = curl_exec($ch);
  15.         //         var_dump($tmpInfo);
  16.         //        exit;
  17.         if (curl_errno($ch)) {
  18.             return false;
  19.         }else{
  20.             // var_dump($tmpInfo);
  21.             return $tmpInfo;
  22.         }
  23.     }


3、根据返回的二进制数据生成图片并上传到自己的服务器


主要代码:
  1. file_put_contents($filepath, $result)

我的是上传到阿里云服务器、就不贴上传部分的代码了。



免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。