您的位置:首页 > 移动开发 > 微信开发

生成微信小程序二维码 PHP

2017-10-24 18:06 645 查看
参考了许多代码,重点是这篇:http://www.php.cn/xiaochengxu-353601.html

/**

* 微信小程序二维码-商家详情

*/

public function appletCode(Request $request)

{

$store_id = $request->input('id','');

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2f90faa515ff788a&secret=c6aad1dd72129264618a8f50c258a463";

//// $url = "https://api.weixin.qq.com/cgi-bin/token";

// $url = "https://cli.api.qesion.com/routine/activity/storeDetail";

$params = ['grant_type'=>'client_credential','appid'=>'wx2f90faa515ff788a','secret'=>'c6aad1dd72129264618a8f50c258a463'];

$access_token = Lcurl::curl($url,$params,'GET',TRUE);

$access_token = json_decode($access_token,true)['access_token'];

$path="pages/shopIndex/shopIndex?store_id=".$store_id;

$width=430;

$post_data='{"path":"'.$path.'","width":'.$width.'}';

$url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;

$result=$this->api_notice_increment($url,$post_data);

$base = base64_encode($store_id);

$config = config('code');

$path =$config['img_path'];

$download_file_name = $base.'.png';

$file_name = $path . $download_file_name;

file_put_contents($file_name, $result);

//// $url=$config['applet_code'].$base;

//// $url=$url.'?store_id='.$store_id;

// $errorCorrectionLevel = "L"; // 纠错级别:L、M、Q、H

// $matrixPointSize = "6"; //生成图片大小 :1到10

// \QRcode::png($url, $file_name, $errorCorrectionLevel, $matrixPointSize,2);

if (file_exists($file_name)) {

$file = fopen($file_name, "r");

Header("Content-type: application/octet-stream");

Header("Accept-Ranges: bytes");

Header("Accept-Length: " . filesize($file_name));

Header("Content-Disposition: attachment; filename=" . $download_file_name);

readfile($file_name);

// 输出文件内容

echo fread($file, filesize($file_name));

fclose($file);

//下载完成后,删除该图片

unlink($file_name);

}

}

protected function send_post($url, $post_data,$method='POST') {

$postdata = http_build_query($post_data);

$options = array(

'http' => array(

'method' => $method, //or GET

'header' => 'Content-type:application/x-www-form-urlencoded',

'content' => $postdata,

'timeout' => 15 * 60 // 超时时间(单位:s)

)

);

$context = stream_context_create($options);

$result = file_get_contents($url, false, $context);

return $result;

}

protected function api_notice_increment($url, $data){

$ch = curl_init();

$header = array("Accept-Charset: utf-8");

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_AUTOREFERER, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$tmpInfo = curl_exec($ch);

// var_dump($tmpInfo);

// exit;

if (curl_errno($ch)) {

return false;

}else{

// var_dump($tmpInfo);

return $tmpInfo;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 微信 二维码