微信接口开发代码(微信开发平台接口)

网友投稿 269 2023-02-20


本篇文章给大家谈谈微信接口开发代码,以及微信开发平台接口对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享微信接口开发代码的知识,其中也会对微信开发平台接口进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

C# 关于调用微信接口的代码

之前公司下达了开发微信只一块的任务,然后我就去网上取了一番经,可是感觉对于我这种微信开发的新手来说,所需要的东西太多,太凌乱了,然后整个人就懵逼了。

费了很长时间才将微信接口调用完成。所以呢,我整理了一下,调用微信接口前需要准备的内容。

1.微信公众平台的appid

2.微信公众平台的secret

3..获取tokenid

4.获取ticket

5.生成签名的随机串

6.生成签名的时间戳

7.生成签名

================================================================================

1.微信公众平台的appid

2.微信公众平台的secret

这两者需要登录到申请的微信公众平台中去获取,建议写在配置文件中

================================================================================

3.获取tokenid

这里是获取微信tokenid的返回对象

注意:在每个微信公众号中获取tokenid的次数是有限的,所以应该将获取到的tokenid储存起来,以便后续使用。我使用的方法是将tokenid存储在数据库中,所以在每次使用之前都要做判断处理

/*tokenid保存方式说明:
*可在数据库中创建表:SysConfig(用户存储项目中的配置数据)
* 字段:
* ConfigKey:用于查询该条数据的key,做为主键
* ConfigValue:存储数据的值
* TypeName:该条配置数据的名称
* Description:说明
* CreateTime:创建时间
* LastModifyTime:上次修改的时间
* AllowEdit:是否可编辑
* LastValue:上一次的值
* tokenid的有效时间是两个小时=7200秒,每重新获取一次就更新一次LastModifyTime的值,将LastModifyTime和当前时间进行比对,如果小于7200秒则可以不用再次获取,反之则需要再次从微信获取。
*/

===================================================================================================

4.获取ticket。需要上一步中获取到的tokenid。

=====================================================================================================

5.生成签名的随机串

====================================================================================================

6.生成签名的时间戳

====================================================================================================

7.生成签名

====================================================================================================

最后可以将这些步骤封装在一个方法中

===================================================================================================

页面上面调用我们上面配置好的内容

===================================================================================================

请求的后台代码

至于需要的接口就去微信公众平台开发者文档中去查看啦。

求一段微信开发新增永久图片素材的接口代码

//素材
const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
const MEDIA_FOREVER_GET_URL = '/material/get_material?';
const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';


/**
* 上传临时素材,有效期为3天(认证后的订阅号可用)
* 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
* 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
* 注意:临时素材的media_id是可复用的!
* @param array $data {"media":'@Path\filename.jpg'}
* @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
* @return boolean|array
*/
public function uploadMedia($data, $type){
   if (!$this-access_token !$this-checkAuth()) return false;
   //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
   $result = $this-http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD_URL.'access_token='.$this-access_token.'type='.$type,$data,true);
   if ($result)
   {
       $json = json_decode($result,true);
       if (!$json || !empty($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return $json;
   }
   return false;
}
/**
* 获取临时素材(认证后的订阅号可用)
* @param string $media_id 媒体文件id
* @param boolean $is_video 是否为视频文件,默认为否
* @return raw data
*/
public function getMedia($media_id,$is_video=false){
   if (!$this-access_token !$this-checkAuth()) return false;
   //原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
   //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
   $url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
   $result = $this-http_get($url_prefix.self::MEDIA_GET_URL.'access_token='.$this-access_token.'media_id='.$media_id);
   if ($result)
   {
       if (is_string($result)) {
           $json = json_decode($result,true);
           if (isset($json['errcode'])) {
               $this-errCode = $json['errcode'];
               $this-errMsg = $json['errmsg'];
               return false;
           }
       }
       return $result;
   }
   return false;
}
/**
* 上传永久素材(认证后的订阅号可用)
* 新增的永久素材也可以在公众平台官网素材管理模块中看到
* 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
* 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
* @param array $data {"media":'@Path\filename.jpg'}
* @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
* @param boolean $is_video 是否为视频文件,默认为否
* @param array $video_info 视频信息数组,非视频素材不需要提供 array('title'='视频标题','introduction'='描述')
* @return boolean|array
*/
public function uploadForeverMedia($data, $type,$is_video=false,$video_info=array()){
   if (!$this-access_token !$this-checkAuth()) return false;
   //#TODO 暂不确定此接口是否需要让视频文件走http协议
   //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
   //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
   //当上传视频文件时,附加视频文件信息
   if ($is_video) $data['description'] = self::json_encode($video_info);
   $result = $this-http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL.'access_token='.$this-access_token.'type='.$type,$data,true);
   if ($result)
   {
       $json = json_decode($result,true);
       if (!$json || !empty($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return $json;
   }
   return false;
}
/**
* 上传永久图文素材(认证后的订阅号可用)
* 新增的永久素材也可以在公众平台官网素材管理模块中看到
* @param array $data 消息结构{"articles":[{...}]}
* @return boolean|array
*/
public function uploadForeverArticles($data){
   if (!$this-access_token !$this-checkAuth()) return false;
   $result = $this-http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$this-access_token,self::json_encode($data));
   if ($result)
   {
       $json = json_decode($result,true);
       if (!$json || !empty($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return $json;
   }
   return false;
}
/**
* 修改永久图文素材(认证后的订阅号可用)
* 永久素材也可以在公众平台官网素材管理模块中看到
* @param string $media_id 图文素材id
* @param array $data 消息结构{"articles":[{...}]}
* @param int $index 更新的文章在图文素材的位置,第一篇为0,仅多图文使用
* @return boolean|array
*/
public function updateForeverArticles($media_id,$data,$index=0){
   if (!$this-access_token !$this-checkAuth()) return false;
   if (!isset($data['media_id'])) $data['media_id'] = $media_id;
   if (!isset($data['index'])) $data['index'] = $index;
   $result = $this-http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPDATE_URL.'access_token='.$this-access_token,self::json_encode($data));
   if ($result)
   {
       $json = json_decode($result,true);
       if (!$json || !empty($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return $json;
   }
   return false;
}
/**
* 获取永久素材(认证后的订阅号可用)
* 返回图文消息数组或二进制数据,失败返回false
* @param string $media_id 媒体文件id
* @param boolean $is_video 是否为视频文件,默认为否
* @return boolean|array|raw data
*/
public function getForeverMedia($media_id,$is_video=false){
   if (!$this-access_token !$this-checkAuth()) return false;
   $data = array('media_id' = $media_id);
   //#TODO 暂不确定此接口是否需要让视频文件走http协议
   //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
   //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
   $result = $this-http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_GET_URL.'access_token='.$this-access_token,self::json_encode($data));
   if ($result)
   {
       if (is_string($result)) {
           $json = json_decode($result,true);
           if (isset($json['errcode'])) {
               $this-errCode = $json['errcode'];
               $this-errMsg = $json['errmsg'];
               return false;
           }
           return $json;
       }
       return $result;
   }
   return false;
}
/**
* 删除永久素材(认证后的订阅号可用)
* @param string $media_id 媒体文件id
* @return boolean
*/
public function delForeverMedia($media_id){
   if (!$this-access_token !$this-checkAuth()) return false;
   $data = array('media_id' = $media_id);
   $result = $this-http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_DEL_URL.'access_token='.$this-access_token,self::json_encode($data));
   if ($result)
   {
       $json = json_decode($result,true);
       if (!$json || !empty($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return true;
   }
   return false;
}
/**
* 获取永久素材列表(认证后的订阅号可用)
* @param string $type 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
* @param int $offset 全部素材的偏移位置,0表示从第一个素材
* @param int $count 返回素材的数量,取值在1到20之间
* @return boolean|array
* 返回数组格式:
* array(
*  'total_count'=0, //该类型的素材的总数
*  'item_count'=0,  //本次调用获取的素材的数量
*  'item'=array()   //素材列表数组,内容定义请参考官方文档
* )
*/
public function getForeverList($type,$offset,$count){
   if (!$this-access_token !$this-checkAuth()) return false;
   $data = array(
       'type' = $type,
       'offset' = $offset,
       'count' = $count,
   );
   $result = $this-http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$this-access_token,self::json_encode($data));
   if ($result)
   {
       $json = json_decode($result,true);
       if (isset($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return $json;
   }
   return false;
}
/**
* 获取永久素材总数(认证后的订阅号可用)
* @return boolean|array
* 返回数组格式:
* array(
*  'voice_count'=0, //语音总数量
*  'video_count'=0, //视频总数量
*  'image_count'=0, //图片总数量
*  'news_count'=0   //图文总数量
* )
*/
public function getForeverCount(){
   if (!$this-access_token !$this-checkAuth()) return false;
   $result = $this-http_get(self::API_URL_PREFIX.self::MEDIA_FOREVER_COUNT_URL.'access_token='.$this-access_token);
   if ($result)
   {
       $json = json_decode($result,true);
       if (isset($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return $json;
   }
   return false;
}
/**
* 上传图文消息素材,用于群发(认证后的订阅号可用)
* @param array $data 消息结构{"articles":[{...}]}
* @return boolean|array
*/
public function uploadArticles($data){
   if (!$this-access_token !$this-checkAuth()) return false;
   $result = $this-http_post(self::API_URL_PREFIX.self::MEDIA_UPLOADNEWS_URL.'access_token='.$this-access_token,self::json_encode($data));
   if ($result)
   {
       $json = json_decode($result,true);
       if (!$json || !empty($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return $json;
   }
   return false;
}
/**
* 上传视频素材(认证后的订阅号可用)
* @param array $data 消息结构
* {
*     "media_id"="",     //通过上传媒体接口得到的MediaId
*     "title"="TITLE",    //视频标题
*     "description"="Description"        //视频描述
* }
* @return boolean|array
* {
*     "type":"video",
*     "media_id":"mediaid",
*     "created_at":1398848981
*  }
*/
public function uploadMpVideo($data){
   if (!$this-access_token !$this-checkAuth()) return false;
   $result = $this-http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_VIDEO_UPLOAD.'access_token='.$this-access_token,self::json_encode($data));
   if ($result)
   {
       $json = json_decode($result,true);
       if (!$json || !empty($json['errcode'])) {
           $this-errCode = $json['errcode'];
           $this-errMsg = $json['errmsg'];
           return false;
       }
       return $json;
   }
   return false;
}


微信开发平台中有个接口是上传多媒体文件,我用的是java 开发的,我怎么样才能在后台实现呢?代码如下:

/**
     * 文件上传到微信服务器
     * @param fileType 文件类型
     * @param filePath 文件路径
     * @return JSONObject
     * @throws Exception
     */
    public static JSONObject send(String fileType, String filePath) throws Exception {  
        String result = null;  
        File file = new File(filePath);  
        if (!file.exists() || !file.isFile()) {  
            throw new IOException("文件不存在");  
        }  
        /** 
        * 第一部分 
        */  
        URL urlObj = new URL("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token="+ getAccess_token() + "type="+fileType+"");  
        HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();  
        con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式  
        con.setDoInput(true);  
        con.setDoOutput(true);  
        con.setUseCaches(false); // post方式不能使用缓存  
        // 设置请求头信息  
        con.setRequestProperty("Connection", "Keep-Alive");  
        con.setRequestProperty("Charset", "UTF-8");  
        // 设置边界  
        String BOUNDARY = "----------" + System.currentTimeMillis();  
        con.setRequestProperty("Content-Type", "multipart/form-data; boundary="+ BOUNDARY);  
        // 请求正文信息  
        // 第一部分:  
        StringBuilder sb = new StringBuilder();  
        sb.append("--"); // 必须多两道线  
        sb.append(BOUNDARY);  
        sb.append("\r\n");  
        sb.append("Content-Disposition: form-data;name=\"file\";filename=\""+ file.getName() + "\"\r\n");  
        sb.append("Content-Type:application/octet-stream\r\n\r\n");  
        byte[] head = sb.toString().getBytes("utf-8");  
        // 获得输出流  
        OutputStream out = new DataOutputStream(con.getOutputStream());  
        // 输出表头  
        out.write(head);  
        // 文件正文部分  
        // 把文件已流文件微信接口开发代码的方式 推入到url中  
        DataInputStream in = new DataInputStream(new FileInputStream(file));  
        int bytes = 0;  
        byte[] bufferOut = new byte[1024];  
        while ((bytes = in.read(bufferOut)) != -1) {  
        out.write(bufferOut, 0, bytes);  
        }  
        in.close();  
        // 结尾部分  
        byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线  
        out.write(foot);  
        out.flush();  
        out.close();  
        StringBuffer buffer = new StringBuffer();  
        BufferedReader reader = null;  
        try {  
        // 定义BufferedReader输入流来读取URL微信接口开发代码的响应  
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));  
        String line = null;  
        while ((line = reader.readLine()) != null) {  
        //System.out.println(line);  
        buffer.append(line);  
        }  
        if(result==null){  
        result = buffer.toString();  
        }  
        } catch (IOException e) {  
        System.out.println("发送POST请求出现异常!" + e);  
        e.printStackTrace();  
        throw new IOException("数据读取异常");  
        } finally {  
        if(reader!=null){  
        reader.close();  
        }  
        }  
        JSONObject jsonObj =new JSONObject(result);  
        return jsonObj;  
    }

微信授权h5网页登录手机端接口代码操作流程

1 第一步:用户同意授权,获取code

2 第二步:通过code换取网页授权access_token

3 第三步:刷新access_token(如果需要)

4 第四步:拉取用户信息(需scope为 snsapi_userinfo)
参考地址:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#2
微信接口调试工具地址:

https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=indextype=%E5%9F%BA%E7%A1%80%E6%94%AF%E6%8C%81form=%E8%8E%B7%E5%8F%96access_token%E6%8E%A5%E5%8F%A3%20/token

如何进行微信二次开发的接口配置

首先百度 “微信公众平台”,注册微信公众账号,注册好后需要通过实名认证(这些具体过程就不写微信接口开发代码了,有很多关于这个的经验)。看到如图所示,出现高级功能时,代表实名认证通过,另有系统通知。
点击高级模式,进入后出现两种模式的选择,分别是编辑模式和开发模式,微信接口开发代码我们选择开发模式。
进入开发者模式后,点击查看文档。
选择 消息接口 下的 消息接口指南 ,翻到页面最下方,下载示例php代码,解压并打开。
回到之前百度bae的编辑页面如图微信接口开发代码
将下载到的微信php示例文件中的内容复制到编辑区域,然后按键盘ctrl+s或者点击页面上的保存按钮。如图,
回到微信的高级模式管理页面,在接口配置信息里面进行设置,URL中填写上一节中第十二步中访问的地址,形式如:.*****.***.com/index.php ,注意要写上后面的index.php,token信息填写weixin,然后点击提交。
页面提示说信息配置成功,并且开发者模式页面变成如图,代表配置成功。
最后一步就是要启动开发者模式,点击图上按钮,启动开发者模式,出现如图效果即可。
完成这些,微信开发的基本工作就做好了,下一步将进行具体程序的编写和讲解。 关于微信接口开发代码和微信开发平台接口的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 微信接口开发代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于微信开发平台接口、微信接口开发代码的信息别忘了在本站进行查找喔。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:usb接口电压测试(usb接口电压怎么测)
下一篇:api接口管理技巧(api接口管理技巧有哪些)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~