PHP生成微信小程序二維碼需要注意兩點:
1、小程序必須是上線的
2、傳入二維碼中需要跳轉的小程序頁面路徑最前面不能加‘/’
具體實現代碼如下:
1 <?php 2 3 namespace app\admin\controller; 4 5 use think\facade\Env; 6 use app\admin\model\Company; 7 8 class Index extends Common { 9 10 public function index() { 11 $res = 1; 12 $qrcode = self::getQrcode($res,'pages/index/index'); 13 dump($qrcode); 14 } 15 /** 16 * 獲取小程序二維碼 17 * @param int $good_id 商品ID 18 * @param varchar $samll_program_url 要跳轉的路徑(小程序路徑) 19 */ 20 protected static function getQrcode($good_id,$samll_program_url) 21 { 22 require_once(Env::get('root_path') . 'vendor/Other/JSSDK.php'); 23 24 $filename = "qrcode_" . $good_id . ".jpg";//要生成的圖片名字 25 26 $IMG_PATH = config('app.public.qrcode');//保存服務器路徑 27 $shop_url = config('app.public.host');//網址 28 if (file_exists('./'.$IMG_PATH .'/'. $filename)) { //存在返回圖片路徑 29 return $shop_url . $IMG_PATH .'/'. $filename; 30 } 31 32 $jssdk = new \JSSDK(config('app.wxapp.app_id'), config('app.wxapp.app_secret')); 33 $access_token = $jssdk->getAccessTokenMinProgram(); 34 $scene = $good_id; 35 $data = '{"scene":"' . $scene . '","page":"' . $samll_program_url . '","width":280}'; 36 $jpg = posturl('https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $access_token, $data);//二進制原始數據 37 38 $res = file_put_contents('./'.$IMG_PATH .'/'. $filename,$jpg); 39 40 return $shop_url . $IMG_PATH .'/'. $filename; 41 } 42 }
JSSDK.php文件內容如下:
1 <?php 2 3 class JSSDK { 4 private $appId; 5 private $appSecret; 6 7 public function __construct($appId, $appSecret) { 8 $this->appId = $appId; 9 $this->appSecret = $appSecret; 10 } 11 12 public function getSignPackage($url = null) { 13 $jsapiTicket = $this->getJsApiTicket(); 14 // 注意 URL 一定要動態獲取,不能 hardcode. 15 if($url === null) 16 { 17 if (isset($_SERVER['HTTP_REFERER'])) { 18 $url = $_SERVER['HTTP_REFERER']; 19 } else { 20 $url = ''; 21 } 22 } 23 $timestamp = time(); 24 $nonceStr = $this->createNonceStr(); 25 26 // 這里參數的順序要按照 key 值 ASCII 碼升序排序 27 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; 28 29 $signature = sha1($string); 30 31 $signPackage = array( 32 "app_id" => $this->appId, 33 "nonceStr" => $nonceStr, 34 "timestamp" => $timestamp, 35 "url" => $url, 36 "signature" => $signature, 37 "rawString" => $string 38 ); 39 return $signPackage; 40 } 41 42 private function createNonceStr($length = 16) { 43 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 44 $str = ""; 45 for ($i = 0; $i < $length; $i++) { 46 $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 47 } 48 return $str; 49 } 50 51 private function getJsApiTicket() { 52 53 $accessToken = $this->getAccessToken(); 54 // 如果是企業號用以下 URL 獲取 ticket 55 // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; 56 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; 57 $res = json_decode($this->getUrl($url),true); 58 if (isset($res['ticket'])) { 59 $ticket = $res['ticket']; 60 } else { 61 $ticket = ''; 62 } 63 64 return $ticket; 65 } 66 67 public function getAccessToken() { 68 69 if ($access_token = Cache::get("access_token")) { 70 return $access_token; 71 } 72 73 // 如果是企業號用以下URL獲取access_token 74 // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; 75 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; 76 77 $res = json_decode($this->getUrl($url),true); 78 79 if (isset($res["access_token"])) { 80 $access_token = $res["access_token"]; 81 return $access_token; 82 } else { 83 return ''; 84 } 85 86 } 87 88 public function getAccessTokenMinProgram() { 89 90 if ($access_token = Cache::get("access_token_min_program")) { 91 return $access_token; 92 } 93 // 如果是企業號用以下URL獲取access_token 94 // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; 95 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; 96 $res = json_decode($this->getUrl($url),true); 97 98 if (isset($res["access_token"])) { 99 $access_token = $res["access_token"]; 100 return $access_token; 101 } else { 102 return ''; 103 } 104 105 } 106 private function getUrl($url){ 107 $ch = curl_init(); 108 curl_setopt($ch, CURLOPT_URL, $url); 109 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 110 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 111 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 112 $output = curl_exec($ch); 113 curl_close($ch); 114 return $output; 115 } 116 117 }
注:如果file_put_contents()報錯,請先檢查文件夾是否創建以及是否有寫入的權限;其次再檢查文件路徑是否正確。
