获取小程序任意页面的小程序码
2018-07-10 本文已影响6人
程大哥T_T
先获取access_token
使用raw方式,测试用postman
![](https://img.haomeiwen.com/i6095563/27046b3e50b0cacb.png)
body填json:
(scene)是参数
{
"path": "pages/index/index",
"scene": ""
}
POST请求:
- url (access_token)替换自己的
这只是其中的一种请求获取的方式,更多的方式查看
上面的是通过postman来获取小程序码,下面的是通过代码获取
public function wxqrcode()
{
$uid = $this->post->uid;
$uid = $this->getuid();
$bigImgPath = '';
$appid='xxxxx';
$secert='xxxxxx';
//处理获取二维码
$tokenurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secert";
$tokeninfo = $this->loadModel("wxapi")->curlget($tokenurl);
$access_token = $tokeninfo[access_token];
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";
$url = $url.$access_token;
$postparam = array(
'scene'=>$uid
);
$qrdata = $this->getcomponent2($url,json_encode($postparam));
$qCodePath = $qrdata;
$qCodeImg = imagecreatefromstring($qCodePath);
list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
// imagecopymerge使用注解
$filename = time().rand(100,900).".jpg";
$path = "../../www/data/upload/diyqr";
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);
// echo json_encode($data);
}
//保存文件
imagedestroy($qCodeImg);
}
/**
* get请求
* @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){
$ch = curl_init(); //用curl发送数据给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,0);
$response = curl_exec($ch);
curl_close( $ch );
$rest = json_decode($response,true);
return $response;
}