PHP微信开发之二维码生成类

这篇文章主要介绍了PHP微信开发之二维码生成类,本文使用微信接口实现二维码的生成,并直接给出示例代码,需要的朋友可以参考下。

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: bin
  5. * Date: 15-1-16
  6. * Time: 上午9:48
  7. */
  8. namespace Home\Common;
  9. // 微信处理类
  10. set_time_limit(30);
  11. class Weixin{
  12. //构造方法
  13. static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?";
  14. static $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";
  15. static $qrcode_get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";
  16. //生成二维码
  17. public function getEwm($wechatid,$fqid,$type = 1){
  18. $wechat = M('Member_public')->where(array('id'=> $wechatid))->find();
  19. $appid = $wechat['appid'];
  20. $secret = $wechat['secret'];
  21. $ACCESS_TOKEN = $this->getToken($appid,$secret);
  22. $url = $this->getQrcodeurl($ACCESS_TOKEN,$fqid,1);
  23. return DownLoadQr($url,time());
  24. }
  25. protected function getQrcodeurl($ACCESS_TOKEN,$fqid,$type = 1){
  26. $url = self::$qrcode_url.'access_token='.$ACCESS_TOKEN;
  27. if($type == 1){
  28. //生成永久二维码
  29. $qrcode= '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$fqid.'}}}';
  30. }else{
  31. //生成临时二维码
  32. $qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$fqid.'}}}';
  33. }
  34. $result = $this->http_post_data($url,$qrcode);
  35. $oo = json_decode($result[1]);
  36. if(!$oo->ticket){
  37. $this->ErrorLogger('getQrcodeurl falied. Error Info: getQrcodeurl get failed');
  38. exit();
  39. }
  40. $url = self::$qrcode_get_url.'ticket='.$oo->ticket.'';
  41. return $url;
  42. }
  43. protected function getToken($appid,$secret){
  44. $ACCESS_TOKEN = file_get_contents(self::$token_url."app);
  45. $ACCESS_TOKEN = json_decode($ACCESS_TOKEN);
  46. $ACCESS_TOKEN = $ACCESS_TOKEN->access_token;
  47. return $ACCESS_TOKEN;
  48. }
  49. protected function http_post_data($url, $data_string) {
  50. $ch = curl_init();
  51. curl_setopt($ch, CURLOPT_POST, 1);
  52. curl_setopt($ch, CURLOPT_URL, $url);
  53. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  54. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  55. 'Content-Type: application/json; charset=utf-8',
  56. 'Content-Length: ' . strlen($data_string))
  57. );
  58. ob_start();
  59. curl_exec($ch);
  60. if (curl_errno($ch)) {
  61. $this->ErrorLogger('curl falied. Error Info: '.curl_error($ch));
  62. }
  63. $return_content = ob_get_contents();
  64. ob_end_clean();
  65. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  66. return array($return_code, $return_content);
  67. }
  68. //下载二维码到服务器
  69. protected function DownLoadQr($url,$filestring){
  70. if($url == ""){
  71. return false;
  72. }
  73. $filename = $filestring.'.jpg';
  74. ob_start();
  75. readfile($url);
  76. $img=ob_get_contents();
  77. ob_end_clean();
  78. $size=strlen($img);
  79. $fp2=fopen('./Uploads/qrcode/'.$filename,"a");
  80. if(fwrite($fp2,$img) === false){
  81. $this->ErrorLogger('dolwload image falied. Error Info: 无法写入图片');
  82. exit();
  83. }
  84. fclose($fp2);
  85. return './Uploads/qrcode/'.$filename;
  86. }
  87. private function ErrorLogger($errMsg){
  88. $logger = fopen('./ErrorLog.txt', 'a+');
  89. fwrite($logger, date('Y-m-d H:i:s')." Error Info : ".$errMsg."\r\n");
  90. }
  91. }