生成微信小程序二维码 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;
}
}
/**
* 微信小程序二维码-商家详情
*/
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版 微信小程序生成带参数小程序二维码图片 php接口+小程序demo
- 微信小程序 PHP生成带参数二维码
- PHP生成微信小程序二维码
- java微信小程序参数二维码生成带背景图加字体(无限生成)
- 获取微信小程序二维码/小程序码 PHP
- 微信小程序请求二维码:用户点击按钮生成带参二维码
- java微信小程序参数二维码生成带背景图加字体(无限生成)
- php微信开发之批量生成带参数的二维码
- 通过数据流处理-微信小程序生成临时二维码
- 【微信小程序 四】二维码生成/扫描二维码
- .NET开发微信小程序-生成二维码/上传图片到服务器
- 微信小程序二维码如何生成?
- .NET开发微信小程序-生成二维码 - 转
- PHP微信开发之二维码生成类
- 如何生成微信小程序正方形二维码(不需请求接口获取)
- 【微信小程序】二维码生成与识别
- php生成微信二维码
- php小程序二维码获取生成图片分享
- 微信小程序-携带参数的二维码条形码生成