您好,欢迎访问一九零五行业门户网

微信公众平台开发获取个性二维码

一、功能介绍在进行推广时,我们可以告诉对方,我们的微信公众账号是什么,客户可以去搜索,然后关注。二维码给我们提供了极大的便捷,只要简单一扫描,即可关注。
如果已经关注过,立刻跳入对话画面。在我们进行推广时,不再是简陋的文字,可以是一个有个性的二维码,想必会很生动。
微信对二维码提供了很好的支持,而且还可以根据需要生成不同场景的二维码。下面我们将介绍如何获取和使用二维码。
注意:限服务号,且进行了微信认证,费用300
二、相关接口为了满足用户渠道推广分析的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。
目前有2种类型的二维码,分别是临时二维码和永久二维码,前者有过期时间,最大为1800秒,但能够生成较多数量,后者无过期时间,数量较少(目前参数只支持1--1000)。两种二维码分别适用于帐号绑定、用户来源统计等场景。
用户扫描带场景值二维码时,可能推送以下两种事件:
如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者。
获取带参数的二维码的过程包括两步,首先创建二维码ticket,然后凭借ticket到指定url换取二维码。
创建二维码ticket每次创建二维码ticket需要提供一个开发者自行设定的参数(scene_id),分别介绍临时二维码和永久二维码的创建二维码ticket过程。
临时二维码请求说明
http请求方式: post url: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=tokenpost数据格式:json post数据例子:{expire_seconds: 1800, action_name: qr_scene, action_info: {scene: {scene_id: 123}}}
永久二维码请求说明
http请求方式: post url: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=tokenpost数据格式:json post数据例子:{action_name: qr_limit_scene, action_info: {scene: {scene_id: 123}}}
参数说明
参数说明
expire_seconds 该二维码有效时间,以秒为单位。 最大不超过1800。
action_name 二维码类型,qr_scene为临时,qr_limit_scene为永久
action_info 二维码详细信息
scene_id 场景值id,临时二维码时为32位整型,永久二维码时最大值为1000
返回说明
正确的json返回结果:
{ticket:gqg28doaaaaaaaaaasxodhrwoi8vd2vpeglulnfxlmnvbs9xl0fuwc1dnmzuvehvmvp4ndnmrnnraaieeslvuqmecacaaa==,expire_seconds:1800}
参数说明
ticket 获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码。
expire_seconds 二维码的有效时间,以秒为单位。最大不超过1800。
错误的json返回示例:
{errcode:40013,errmsg:invalid appid}
全局返回码说明
使用网页调试工具调试该接口
通过ticket换取二维码获取二维码ticket后,开发者可用ticket换取二维码图片。请注意,本接口无须登录态即可调用。
请求说明
http get请求(请使用https协议) https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=ticket
返回说明
ticket正确情况下,http 返回码是200,是一张图片,可以直接展示或者下载。
http头(示例)如下:
accept-ranges:bytes cache-control:max-age=604800connection:keep-alive content-length:28026content-type:image/jpg date:wed, 16 oct 2013 06:37:10 gmt expires:wed, 23 oct 2013 14:37:10 +0800server:nginx/1.4.1
错误情况下(如ticket非法)返回http错误码404。
三、具体实现 依然基于之前的机器人案例进行功能添加,直接看代码。
/// <summary>     /// 二维码管理者     /// </summary>     public class dimensionalcodemanager     {         /// <summary>         /// 临时二维码地址         /// </summary>         /// 使用string.format时,报:字符串格式错误,因为其中有{         //private const string temp_url = {\expire_seconds\: 1800, \action_name\: \qr_scene\, \action_info\: {\scene\: {\scene_id\: {0}}}};         /// <summary>         /// 解决办法,将原有字符串中的一个{用两个{代替         /// </summary>         private const string temp_json_data = {{\expire_seconds\: 1800, \action_name\: \qr_scene\, \action_info\: {{\scene\: {{\scene_id\: {0}}}}}}};         /// <summary>         /// 永久二维码地址         /// </summary>         private const string permanent_url = {{\action_name\: \qr_limit_scene\, \action_info\: {{\scene\: {{\scene_id\: {0}}}}}}};         /// <summary>         /// 获取ticket的url         /// </summary>         private const string get_ticket_url =  https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0};         /// <summary>         /// 获取二维码url         /// </summary>         private const string get_code_url = https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0};         /// <summary>         /// 根据场景id获取ticket         /// </summary>         /// <param name="sceneid">场景id</param>         /// <param name="istemp">是否是临时二维码</param>         /// <returns></returns>         private static string getticket(int sceneid, bool istemp)         {             string result = null;             string data = string.empty;             if (istemp)             {                 data = string.format(temp_json_data, sceneid.tostring());             }             else             {                 if (sceneid > 0 && sceneid <= 1000)                 {                     data = string.format(permanent_url, sceneid);                 }                 else                 {                     //scene_id不合法                     return null;                 }             }             string ticketjson = httputility.getdata(string.format(get_ticket_url,context.accesstoken));             xdocument doc = xmlutility.parsejson(ticketjson, root);             xelement root = doc.root;             if (root != null)             {                 xelement ticket = root.element(ticket);                 if (ticket != null)                 {                     result = ticket.value;                 }             }             return result;         }         /// <summary>         /// 创建临时二维码         /// </summary>         /// <param name="sceneid">场景id,int类型</param>         /// <returns></returns>         public static string generatetemp(int sceneid)         {             string ticket = getticket(sceneid,true);             if (ticket == null)             {                 return null;             }             return httputility.getdata(string.format(get_code_url, ticket));         }         /// <summary>         /// 创建临时二维码         /// </summary>         /// <param name="sceneid">场景id,int类型</param>         /// <returns></returns>         public static string generatepermanent(int sceneid)         {             string ticket = getticket(sceneid, false);             if (ticket == null)             {                 return null;             }             return httputility.getdata(string.format(get_code_url, ticket));         }     }
更多微信公众平台开发获取个性二维码 。
其它类似信息

推荐信息