第一种:获取小程序二维码,限制数量100000个
function qrcode(){
$id = 2;
$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$this->access_token();
$param = json_encode(array("path"=>"pages/tangshi/index?shop_id=".session('shop.id').'&zhuotai_id='.$id,"width"=> 150));
$result = $this->httpRequest($qcode,$param,"POST");
$name = 'static/qrcode/'.time().rand(111111,999999).'.png';
$s = file_put_contents($name,$result);
$base64_image ="data:image/jpeg;base64,".base64_encode($result );
return $name;
}
function httpRequest($url,$data='',$method='GET'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST')
{
curl_setopt($curl, CURLOPT_POST, 1);
if ($data !='')
{
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
第二种:获取不限数量的小程序二维码
/*
* $code 需要放入链接的参数
* $type 需要放入链接的参数
* $httpuser == $_SERVER['HTTP_USER_AGENT']
*/
function qrcode($code,$type,$httpuser){
try{
$qcode ="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$this->access_token();
$scene = 'type='.$type.'&code='.explode('/',$code)[1];//参数长度不能超过32个字符
$param = json_encode(array("path"=>"pages/goodsdetail/goodsdetail","scene"=>$scene,"width"=> 150));
$result = $this->httpRequest($qcode,$param,"POST",$httpuser);
//因为我是在队列中生成,所以文件目录加public
$name = 'public/static/goodsqrcode/'.$code.'.png';
//我的保存目录是两层,所以需要把$code分开,直接保存固定路径则不需要
$s = $this->mkdirs('public/static/goodsqrcode/'.explode('/',$code)[0]);
$s = file_put_contents($name,$result);
$base64_image ="data:image/jpeg;base64,".base64_encode($result );
}catch(\Exception $e){
abort(config('error_log_code'),$e->getMessage());
}
return str_replace('public/','',$name); //不是队列中生成直接 return $name
}
//请求生成二维码
function httpRequest($url,$data='',$method='GET',$httpuser){
try{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT,$httpuser);
// curl_setopt($curl, CURLOPT_USERAGENT,CURLOPT_USERAGENT);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST')
{
curl_setopt($curl, CURLOPT_POST, 1);
if ($data !='')
{
curl_setopt($curl, CURLOPT_POSTFIELDS,$data); //$data == $_SERVER['HTTP_USER_AGENT']
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}catch(\Exception $e){
abort(config('error_log_code'),$e->getMessage());
// var_dump($e->getMessage());die;
}
}
function mkdirs($dir, $mode = 0777)
{
if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
if (!$this->mkdirs(dirname($dir), $mode)) return FALSE;
return @mkdir($dir, $mode);
}