标签:client create 网址 注意 require ant exec function sdk
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()报错,请先检查文件夹是否创建以及是否有写入的权限;其次再检查文件路径是否正确。
标签:client create 网址 注意 require ant exec function sdk
原文地址:https://www.cnblogs.com/Jessie-candy/p/13280537.html