先獲取access_token
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的密鑰
使用raw方式,測(cè)試用postman
請(qǐng)求
body填json:
(scene)是參數(shù)
{
"path": "pages/index/index",
"scene": ""
}
POST請(qǐng)求:
- url (access_token)替換自己的
這只是其中的一種請(qǐng)求獲取的方式且预,更多的方式查看
上面的是通過(guò)postman來(lái)獲取小程序碼送丰,下面的是通過(guò)代碼獲取
public function getWxappQrCode($id)
{
$appid='xxxx';
$secert='xxxxxxx';
//處理獲取二維碼
$tokenurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secert";
$tokeninfo = $this->curlget($tokenurl);
$access_token = $tokeninfo['access_token'];
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";
$url = $url.$access_token;
$postparam = array(
'scene'=>$id
);
$qrdata = $this->getcomponent2($url,json_encode($postparam));
$qCodePath = $qrdata;
$qCodeImg = imagecreatefromstring($qCodePath);
// list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
// list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodeImg);
// imagecopymerge使用注解
$filename = time().rand(100,900).".jpg";
$path =PATH_UPLOAD."qrcode";
if (file_exists($path))
{
imagejpeg($qCodeImg,$path."/".$filename);
$p = URL."/www/data/upload/diyqr/";
$fn = $filename;
$data['img'] = $p.$fn;
ob_end_clean();
header("content-disposition:attachment;filename=wxqrcode.png");
header("content-length:" . filesize($path.'/'.$filename));
readfile($path.'/'.$filename);
}
//保存文件
imagedestroy($qCodeImg);
}
/**
* get請(qǐng)求
* @param $url
* @return mixed
*/
public function curlget($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
$response = curl_exec($ch);
curl_close( $ch );
$rest = json_decode($response,true);
return $rest;
}
public function getcomponent2($url,$postdata){
$header[] = "Content-type: text/xml"; // 改為數(shù)組解決
$ch = curl_init(); //用curl發(fā)送數(shù)據(jù)給api
// curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
$response = curl_exec($ch);
curl_close( $ch );
$rest = json_decode($response,true);
return $response;
}