php 生成小程序二维码
2019-01-22 本文已影响0人
昱弟丶
//获取access_token
public function gettoken()
{
$APPID = "wx**************";
$APPSECRET = "*******************************";
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
$json = $this->httpRequest($access_token);
return json_decode($json,true);
}
//curl
public 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;
}
//生成二维码
public function qrcode($shopid,$userid)
{
$ACCESS_TOKEN = $this->gettoken();
//dump($ACCESS_TOKEN);die;
$puthc = '/pages/product/detail?pro_id='.$shopid.'&userid='.$userid;//小程序的路径 可以带参数
$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$ACCESS_TOKEN['access_token'];
$param = json_encode(array("path"=>$puthc,"width"=> 150));
$result = $this->httpRequest( $qcode,$param,"POST");
//dump($result);die;
$puth = "public/qrcode/".time().rand(100000,999999).'.png';
//$base64_image ="data:image/jpeg;base64,".base64_encode($result);
file_put_contents($puth,$result);
return $puth;
}