WeChat public platform development Upload and download multimedia files

高洛峰
Release: 2017-02-22 15:40:44
Original
4538 people have browsed it

WeChat public accounts will use media_id to call related files when replying to pictures, voices, and videos. Many friends have asked how to develop and implement this. This article will introduce how to upload and download multimedia files during the development process of WeChat public platform.

1. Upload multimedia files

Official accounts can call this interface to upload pictures, voices, videos and other files to the WeChat server. After uploading, the server will return Corresponding media_id, the official account can obtain multimedia based on this media_id. Please note that media_id is reusable, and http protocol is required to call this interface.

Usually, file upload is performed through html forms, but through CURL, you can directly submit the POST form on the server side without going through the browser to complete the file upload function.

It should be noted that the file name must be complete absolute path, and the absolute path needs to be preceded by "@" to distinguish it.

On a Windows server, the format example is: "@F:\israel\upload\winter.jpg", and on a Linux server, the format example is: "@home/israel/upload/winter.jpg" ".

http request method: POST/FORM

http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE

Parameter description

##type is media

Parameter

Is it necessary

Description

##access_token

is

Call interface credentials

## media file type, including image, voice, video and thumbnail

is the media file identifier in

form-data, including filename, filelength, content- type and other information

Upload pictures

微信公众平台开发  上传下载多媒体文件 Return results

{    "type": "image",
    "media_id": "QQ9nj-7ctrqA8t3WKU3dQN24IuFV_516MfZRZNnQ0c-BFVkk66jUkPXF49QE9L1l",
    "created_at": 1389793969}
Copy after login

Upload voice

微信公众平台开发  上传下载多媒体文件 Return results

{    "type": "voice",
    "media_id": "5Idx79V9E3XfBCz_A50gr1a1_klgPpJnb_eq73yz0bn-prhIsNlwI3n6jQgshmWk",
    "created_at": 1389794760}
Copy after login

Upload video

微信公众平台开发  上传下载多媒体文件

Return results

{    "type": "video",
    "media_id": "Jm-Wq0nXtA_oN1qNydQRP03dCsB0R2t5gCHDM3QNkBmMRE1WBaorVJNQTBRHvK9-",
    "created_at": 1389794768}
Copy after login

Upload abbreviation Sketch

Return result微信公众平台开发  上传下载多媒体文件

{    "type": "thumb",
    "thumb_media_id": "2RhP0caRKHVOmZO5AKelHkK--vqPPwgUaRp5-WE63dvmmPRWiYVKgvNblIp_gv79",
    "created_at": 1389794771}
Copy after login

Parameter description

Parameter Description

type

Media file types include image, voice, video and thumbnail, mainly Thumbnails for video and music formats)

media_id

After the media file is uploaded, it is unique when obtained Identification

created_at

Media file upload timestamp

注意事项

上传的多媒体文件有格式和大小限制,如下:

Ÿ  图片(image): 128K,支持JPG格式

Ÿ  语音(voice):256K,播放长度不超过60s,支持AMR\MP3格式

Ÿ  视频(video):1MB,支持MP4格式

Ÿ  缩略图(thumb):64KB,支持JPG格式

媒体文件在后台保存时间为3天,即3天后media_id失效。对于需要重复使用的多媒体文件,可以每3天循环上传一次,更新media_id。

 

二、下载多媒体文件

公众号可调用本接口来获取多媒体文件。请注意,视频文件不支持下载,调用该接口需http协议。

下载文件使用获取图片数据,写入新文件的方法。

http请求方式: GET

http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID

参数说明

参数

是否必须

说明

access_token

调用接口凭证

media_id

媒体文件ID

 

下载图片

代码实现

微信公众平台开发  上传下载多媒体文件

返回HTTP头示例如下

{    "url": "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=My4oqLEyFVrgFF-XOZagdvbTt9XywYjGwMg_GxkPwql7-f0BpnvXFCOKBUyAf0agmZfMChW5ECSyTAgAoaoU2WMyj7aVHmB17ce4HzLRZ3XFTbm2vpKt_9gYA29xrwIKpnvH-BYmNFSddt7re5ZrIg&media_id=QQ9nj-7ctrqA8t3WKU3dQN24IuFV_516MfZRZNnQ0c-BFVkk66jUkPXF49QE9L1l",
    "content_type": "image/jpeg",
    "http_code": 200,
    "header_size": 308,
    "request_size": 316,
    "filetime": -1,
    "ssl_verify_result": 0,
    "redirect_count": 0,
    "total_time": 1.36,
    "namelookup_time": 1.016,
    "connect_time": 1.078,
    "pretransfer_time": 1.078,
    "size_upload": 0,
    "size_download": 105542,
    "speed_download": 77604,
    "speed_upload": 0,
    "download_content_length": 105542,
    "upload_content_length": 0,
    "starttransfer_time": 1.141,
    "redirect_time": 0}
Copy after login

 

下载语音

代码实现

微信公众平台开发  上传下载多媒体文件

返回HTTP头如下

{    "url": "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=My4oqLEyFVrgFF-XOZagdvbTt9XywYjGwMg_GxkPwql7-f0BpnvXFCOKBUyAf0agmZfMChW5ECSyTAgAoaoU2WMyj7aVHmB17ce4HzLRZ3XFTbm2vpKt_9gYA29xrwIKpnvH-BYmNFSddt7re5ZrIg&media_id=5Idx79V9E3XfBCz_A50gr1a1_klgPpJnb_eq73yz0bn-prhIsNlwI3n6jQgshmWk",
    "content_type": "audio/amr",
    "http_code": 200,
    "header_size": 306,
    "request_size": 316,
    "filetime": -1,
    "ssl_verify_result": 0,
    "redirect_count": 0,
    "total_time": 0.125,
    "namelookup_time": 0.031,
    "connect_time": 0.063,
    "pretransfer_time": 0.063,
    "size_upload": 0,
    "size_download": 10470,
    "speed_download": 83760,
    "speed_upload": 0,
    "download_content_length": 10470,
    "upload_content_length": 0,
    "starttransfer_time": 0.125,
    "redirect_time": 0}
Copy after login

可以看出,MP3的语音格式被压缩成AMR格式了。

 

下载缩略图

实现代码

微信公众平台开发  上传下载多媒体文件

返回HTTP头如下

{    "url": "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=My4oqLEyFVrgFF-XOZagdvbTt9XywYjGwMg_GxkPwql7-f0BpnvXFCOKBUyAf0agmZfMChW5ECSyTAgAoaoU2WMyj7aVHmB17ce4HzLRZ3XFTbm2vpKt_9gYA29xrwIKpnvH-BYmNFSddt7re5ZrIg&media_id=2RhP0caRKHVOmZO5AKelHkK--vqPPwgUaRp5-WE63dvmmPRWiYVKgvNblIp_gv79",
    "content_type": "image/jpeg",
    "http_code": 200,
    "header_size": 306,
    "request_size": 316,
    "filetime": -1,
    "ssl_verify_result": 0,
    "redirect_count": 0,
    "total_time": 0.094,
    "namelookup_time": 0,
    "connect_time": 0.047,
    "pretransfer_time": 0.047,
    "size_upload": 0,
    "size_download": 6057,
    "speed_download": 64436,
    "speed_upload": 0,
    "download_content_length": 6057,
    "upload_content_length": 0,
    "starttransfer_time": 0.094,
    "redirect_time": 0}
Copy after login

 至于回复相应的消息,就是利用消息接口或者客服接口,构造成相应的消息就行了,和构造文本,图文消息的方法是一样的。

更多微信公众平台开发  上传下载多媒体文件 相关文章请关注PHP中文网!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template