微信生成二维码
扫描这个二维码,可进行关注服务号。
这样可生成不同二维码,来统计不同来源,回复不同的消息,以及我后面要做的自动分组。
下面是获取二维码的代码。
/**************
* 微信生成关注二维码
**************/
//第一步:获取access_token
$appid = ''; // 是应用唯一标识
$appSecret = '';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appSecret";
$result = https_request($url);
$result = json_decode($result, true);
$access_token = empty($result['access_token']) ? '' : $result['access_token'];
if(!empty($access_token))
{
//第二步:创建二维码ticket接口 /qrcode/create
$url2 = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
//渠道值 scene_id
$strJson = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}';
$result2 = https_request($url2,$strJson);
$result2 = json_decode($result2, true);
$ticket = empty($result2['ticket']) ? '' : $result2['ticket'];
//第三步获取二维码
if (!empty($ticket)) {
$url3 = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=$ticket";
//$result3 = https_request($url3);
//$result3 = json_decode($result3, true);
//header("Content-Type:image/jpg");
//imagejpeg($result2['url']);
echo ' <;img src="'.$url3.'" />';
}
}
function https_request($url, $data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}