When a public account replies to images, audios, and videos, it uses media_id to call related files. Many friends have asked how to develop and implement this function. This article describes how to upload and download multimedia files during public platform development. When a public account replies to images, audios, and videos, it uses media_id to call related files. Many friends have asked how to develop and implement this function. This article describes how to upload and download multimedia files during public platform development.
1. Upload multimedia files
You can call this interface to upload images, audios, videos, and other files to the server. after the files are uploaded, the server returns the corresponding media_id. the public account can then be used to obtain multimedia files based on the media_id. Please note that media_id is reusable and the http protocol is required to call this interface.
Generally, files are uploaded through html forms, but CURL can directly submit the form POST on the server without passing through the browser to complete the file upload function.
Note that the file name must be a complete absolute path, and "@" must be added before the absolute path to differentiate.
On a Windows Server, the format example is "@ F: \ israel \ upload \ winter.jpg". 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
Parameters |
Required? |
Description |
Access_token |
Yes |
Interface call credential |
Type |
Yes |
Media file types, including image, voice, video, and thumb) |
Media |
Yes |
The ID of a media file in form-data, including filename, filelength, and content-type. |
Upload images
Returned results
{ "type": "image", "media_id": "QQ9nj-7ctrqA8t3WKU3dQN24IuFV_516MfZRZNnQ0c-BFVkk66jUkPXF49QE9L1l", "created_at": 1389793969}
Upload voice
Returned results
{ "type": "voice", "media_id": "5Idx79V9E3XfBCz_A50gr1a1_klgPpJnb_eq73yz0bn-prhIsNlwI3n6jQgshmWk", "created_at": 1389794760}
Upload video
Returned results
{ "type": "video", "media_id": "Jm-Wq0nXtA_oN1qNydQRP03dCsB0R2t5gCHDM3QNkBmMRE1WBaorVJNQTBRHvK9-", "created_at": 1389794768}
Upload thumbnails
Returned results
{ "type": "thumb", "thumb_media_id": "2RhP0caRKHVOmZO5AKelHkK--vqPPwgUaRp5-WE63dvmmPRWiYVKgvNblIp_gv79", "created_at": 1389794771}
Parameter description
Parameters |
Description |
Type |
Media file types, including image, voice, video, and thumb. They are mainly used for thumbnails of videos and music formats) |
Media_id |
Unique identifier of a media file after it is uploaded |
Created_at |
Media file Upload timestamp |
Notes
The formats and sizes of uploaded multimedia files are limited as follows:
Image: 128 KB. JPG format is supported.
Voice (voice): 256 K. The playback length cannot exceed 60 s. It supports the AMR \ MP3 format.
Video: 1 MB. MP4 format is supported.
Thumbnail (thumb): 64 KB, JPG format supported
The media file is saved in the background for three days, that is, the media_id is invalid three days later. For multimedia files that need to be reused, you can upload them cyclically every three days to update media_id.
II. download multimedia files
You can call this interface to obtain multimedia files. Note that video files cannot be downloaded. http is required to call this interface.
To download a file, use the method of getting image data and writing it to a new file.
Http request method: GET
Http://file.api.weixin.qq.com/cgi-bin/media/get? Access_token = ACCESS_TOKEN & media_id = MEDIA_ID
Parameter description
Parameters |
Required? |
Description |
Access_token |
Yes |
Interface call credential |
Media_id |
Yes |
Media File ID |
Download images
Code implementation
Return HTTP header example:
{ "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}
Download Voice
Code implementation
The returned HTTP header is as follows:
{ "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}
It can be seen that the audio format of MP3 is compressed into AMR format.
Download thumbnail
Implementation code
The returned HTTP header is as follows:
{ "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}
To reply to a message, you can use the message interface or customer service interface to construct the corresponding message. the method for constructing text and text messages is the same.
For more articles related to public platform development and uploading and downloading multimedia files, please follow the PHP Chinese network!