一,上传永久图片素材,可以用以下2种方式:
1.公众号素材管理中直接上传
2.发送POST请求
使用curl命令上传示例:
curl -F media=@pro_nlp.jpg "http://file.api.weixin.qq.com/cgi-bin/material/add_material?access_token=14_3-OW6zGI-yD1LzYlhovytEU81rqH6gDIrYimb4xb_Jriwizb_5XPiaqP7PwCPrMICJ6COq0LS5Rrjxbm0NXpd_C3vhQWrbVMT_iniCvmMwPDfdO1Ekc0pFdlf78PHTgAEAEWI"
使用Python脚步上传示例:
# -*- coding: utf-8 -*-
# filename: media.py
#from basic import Basic
import urllib2
import poster.encode
from poster.streaminghttp import register_openers
class Media(object):
def __init__(self):
register_openers()
#上传图片
def uplaod(self, accessToken, filePath, mediaType):
openFile = open(filePath, "rb")
param = {'media': openFile}
postData, postHeaders = poster.encode.multipart_encode(param)
#postUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken + "&type=" + mediaType
postUrl = "http://file.api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken + "&type=" + mediaType
request = urllib2.Request(postUrl, postData, postHeaders)
urlResp = urllib2.urlopen(request)
print(urlResp.read())
if __name__ == '__main__':
myMedia = Media()
accessToken = "14_s9rzbBYHo-eI-hM10l4PuoPp2KSW7obCG3yhFpUn6jhxJlo_d-HC37c8NqUD8Ydfy1mnhD0nNCOgMPLwMb5sIsk6PlhAGatPO6pPhr3mvfOwHH8RgFJ7zCq_iZHb-Kubt8xWqzwn2hK4rQ5pBVPiAAAHBV"
filePath = "/home/ytkj/temp/pro_nlp.jpg" #请安实际填写
mediaType = "image"
myMedia.uplaod(accessToken, filePath, mediaType)
二,获取素材的MediaID
1.上传时可以直接得到素材的mediaid
2.使用接口获取素材列表
# -*- coding: utf-8 -*-
# filename: material.py
import urllib2
import json
import poster.encode
from poster.streaminghttp import register_openers
class Material(object):
def __init__(self):
register_openers()
#上传
def uplaod(self, accessToken, filePath, mediaType):
openFile = open(filePath, "rb")
fileName = "hello"
param = {'media': openFile, 'filename': fileName}
#param = {'media': openFile}
postData, postHeaders = poster.encode.multipart_encode(param)
postUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=%s" % (accessToken, mediaType)
request = urllib2.Request(postUrl, postData, postHeaders)
urlResp = urllib2.urlopen(request)
print urlResp.read()
#下载
def get(self, accessToken, mediaId):
postUrl = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=%s" % accessToken
postData = "{ \"media_id\": \"%s\" }" % mediaId
urlResp = urllib2.urlopen(postUrl, postData)
headers = urlResp.info().__dict__['headers']
if ('Content-Type: application/json\r\n' in headers) or ('Content-Type: text/plain\r\n' in headers):
jsonDict = json.loads(urlResp.read())
print jsonDict
else:
buffer = urlResp.read() # 素材的二进制
mediaFile = file("test_media.jpg", "wb")
mediaFile.write(buffer)
print "get successful"
#删除
def delete(self, accessToken, mediaId):
postUrl = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=%s" % accessToken
postData = "{ \"media_id\": \"%s\" }" % mediaId
urlResp = urllib2.urlopen(postUrl, postData)
print urlResp.read()
#获取素材列表
def batch_get(self, accessToken, mediaType, offset=0, count=20):
postUrl = ("https://api.weixin.qq.com/cgi-bin/material"
"/batchget_material?access_token=%s" % accessToken)
postData = ("{ \"type\": \"%s\", \"offset\": %d, \"count\": %d }"
% (mediaType, offset, count))
urlResp = urllib2.urlopen(postUrl, postData)
print urlResp.read()
if __name__ == '__main__':
myMaterial = Material()
accessToken = "14_-GQ-SnXSuyd-LZ0Q-iy0y4-Xlm0rGtwaRjf5C6UIjMGWqeQD1gM9lC8OL1ArLoT9fr59-ff1pZcj_5E9uUXbFY77cGiVvPYq6RoZMWeZpjEPqIW5cwaT5YVgkscaV9lTYpIcd8Ocla2MJiBfPOUaAEABAG"
mediaType = "image"
myMaterial.batch_get(accessToken, mediaType)
三,公众号回复消息格式
1.文字
2.图片
3.其他参考,注意微信开发者文档中有错误,回复消息格式中多了空格
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140543