扫码关注微信公众号

public function get_access_token()
{ 
	$access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". $this->appid ."&secret=".$this->secret;
	$access_token_json = $this->https_request($access_token_url);//自定义函数
	$access_token_array = json_decode($access_token_json,true); 
	$access_token = $access_token_array['access_token'];  
	
	return $access_token;
} 
public function https_request($url, array $params = array())//访问url返回结果
{
	$data_string = json_encode($params);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
	curl_setopt($ch, CURLOPT_HTTPHEADER,
	array(
	    'Content-Type: application/json'
	)
	);
	$data = curl_exec($ch);
	curl_close($ch);
	return ($data);
}
public function code()
{
	$access_token = $this->get_access_token();
	$rs = $this->get_qrcode($access_token, 123);
	$ticket = $rs['ticket'];
	$qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $ticket . "";
	///打印二维码显示
	$this->assign('qrcode', $qrcode);
}
//生成二维码
public function get_qrcode($access_tokens,$orderId)
{
    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" .$access_tokens . "";
//生成二维码需要的参数
    $qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": ' . $orderId . '}}}';
    $momo = json_decode($qrcode, true);

    $result = $this->https_request($url, $momo);
    $rs = json_decode($result, true);
    return $rs;
}

你可能感兴趣的:(微信,PHP,微信公众号,PHP)