PHP小程序 获取二维码

//获取token
public function getAccessToken($appId,$appSecret)
{
	// 请求API获取 access_token
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
	$result = $this->get($url);
	$data = json_decode($result, true);
	$access_token = $data['access_token'];
	
	return $access_token ;
}

public function get($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

public function post($url, $data = [])
{
    $header = [
        'Content-type: application/json;'
    ];
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_POST, TRUE);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

//获取二维码,并保存
public function getQrcode($appId,$appSecret)
{
    // 微信接口url
    $access_token = $this->getAccessToken($appId,$appSecret);
    $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$access_token}";
    // 构建请求
    $data = ['scene'=>'user_id:140','width'=>530,'page'=>'pages/index/index'];
    // 返回结果
    $result = $this->post($url, json_encode($data, JSON_UNESCAPED_UNICODE));
    
    if (!strpos($result, 'errcode')) {
    	$savePath = "aa.png";
        file_put_contents($savePath, $result);
    	return $savePath;
    }
    
}

//调试通过小程序编辑器,选择二维码
PHP小程序 获取二维码_第1张图片
在app.js文件里 onLaunch里获取参数。
在这里插入图片描述

你可能感兴趣的:(php,小程序)