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

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

发布:2017-12-01 15:14浏览: 来源:网络 作者:tianshu

官方获取小程序页面API 由于小程序参数二维码API提供的帮助有限,以下是我对该功能的一些理解 我主要是通过thinkphp后台接口实现,代码如下: 1、先获取ACCESS_TOKEN: 纯文本查看 复制代码$tokenUrl=https: ...

 
 
 

官方获取小程序页面API 
QQ图片20170217192918.png

由于小程序参数二维码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.  
  3. $getArr=array();
  4.  
  5. $tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));
  6.  
  7.  
  8.  
  9. $access_token=$tokenArr->access_token;

send_post:

 

		
  1. function send_post($url, $post_data,$method='POST') {
  2.  
  3. $postdata = http_build_query($post_data);
  4.  
  5. $options = array(
  6.  
  7. 'http' => array(
  8.  
  9. 'method' => $method, //or GET
  10.  
  11. 'header' => 'Content-type:application/x-www-form-urlencoded',
  12.  
  13. 'content' => $postdata,
  14.  
  15. 'timeout' => 15 * 60 // 超时时间(单位:s)
  16.  
  17. )
  18.  
  19. );
  20.  
  21. $context = stream_context_create($options);
  22.  
  23. $result = file_get_contents($url, false, $context);
  24.  
  25.  
  26.  
  27.  
  28.  
  29. return $result;
  30.  
  31. }

2、生成二维码:

 

		
  1. $path="pages/index?query=1";
  2.  
  3. $width=430;
  4.  
  5. $post_data='{"path":"'.$path.'","width":'.$width.'}';
  6.  
  7.  
  8.  
  9. $url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
  10.  
  11.  
  12.  
  13.  
  14.  
  15. $result=$this->api_notice_increment($url,$post_data);

api_notice_increment:

 

		
  1. function api_notice_increment($url, $data){
  2.  
  3. $ch = curl_init();
  4.  
  5. $header = "Accept-Charset: utf-8";
  6.  
  7. curl_setopt($ch, CURLOPT_URL, $url);
  8.  
  9. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  10.  
  11. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  12.  
  13. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  14.  
  15. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  16.  
  17. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  18.  
  19. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  20.  
  21. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  22.  
  23. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  24.  
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  26.  
  27. $tmpInfo = curl_exec($ch);
  28.  
  29. // var_dump($tmpInfo);
  30.  
  31. // exit;
  32.  
  33. if (curl_errno($ch)) {
  34.  
  35. return false;
  36.  
  37. }else{
  38.  
  39. // var_dump($tmpInfo);
  40.  
  41. return $tmpInfo;
  42.  
  43. }
  44.  
  45. }

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

主要代码:

 

		
  1. file_put_contents($filepath, $result)

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






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