獲取帶參數的小程序碼
// 該代碼用的yii框架
public function actionQr(){
$host = $http.$_SERVER['HTTP_HOST'];
$file_name = 'qr_gc_'.$id.'.png';
//該判斷爲判斷是否生成過二維碼,存在直接使用
if(file_exists('./qrimage/'.$file_name)){
return json_encode(['code'=>1, 'status'=>'success', 'message'=>'獲取二維碼成功','url'=>$host.'/qrimage/'.$file_name]);
}else{
$getaccurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2ad7a42**00990ca&secret=bdab7201012******ab7b00b0c4cca";
$data = file_get_contents($getaccurl);
$access_token = json_decode($data,true);
$data=["path"=>"pages/signSuccess/signSuccess?id=".$id,"width"=>430,'auto_color'=>true/*,'is_hyaline'=>true*/];
$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token['access_token'];
$result = $this->api_notice_increment($url,json_encode($data));
$base64_image =base64_encode( $result );
//保存圖片到服務器
$base64_image = $this->saveImageToLocal($base64_image,$file_name);
return json_encode(['code'=>1, 'status'=>'success', 'message'=>'獲取二維碼成功','url'=>$host.$base64_image]);
}
}
//發起請求
function api_notice_increment($url, $data){
$ch = curl_init();
$header = "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);
if (curl_errno($ch)) {
return false;
}else{
return $tmpInfo;
}
}
function saveImageToLocal($image,$filename)
{
$path = './qrimage/'.$filename;
$r = file_put_contents($path,base64_decode($image));
if($r>0){
return ltrim($path,'.');
}else{
return false;
}
}