


WeChat public platform development Upload and download multimedia files
Feb 22, 2017 pm 03:40 PMWeChat 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
Parameter |
Is it necessary |
Description |
##access_token | is | Call interface credentials |
is | ## 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}
Upload voice
Return results
{????"type":?"voice", ????"media_id":?"5Idx79V9E3XfBCz_A50gr1a1_klgPpJnb_eq73yz0bn-prhIsNlwI3n6jQgshmWk", ????"created_at":?1389794760}
Upload video
{????"type":?"video", ????"media_id":?"Jm-Wq0nXtA_oN1qNydQRP03dCsB0R2t5gCHDM3QNkBmMRE1WBaorVJNQTBRHvK9-", ????"created_at":?1389794768}
Upload abbreviation Sketch
Return result
{????"type":?"thumb", ????"thumb_media_id":?"2RhP0caRKHVOmZO5AKelHkK--vqPPwgUaRp5-WE63dvmmPRWiYVKgvNblIp_gv79", ????"created_at":?1389794771}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 | ||||||||
注意事項(xiàng) 上傳的多媒體文件有格式和大小限制,如下: ?? 圖片(image): 128K,支持JPG格式 ?? 語音(voice):256K,播放長(zhǎng)度不超過60s,支持AMR\MP3格式 ?? 視頻(video):1MB,支持MP4格式 ?? 縮略圖(thumb):64KB,支持JPG格式 媒體文件在后臺(tái)保存時(shí)間為3天,即3天后media_id失效。對(duì)于需要重復(fù)使用的多媒體文件,可以每3天循環(huán)上傳一次,更新media_id。 ? 二、下載多媒體文件 公眾號(hào)可調(diào)用本接口來獲取多媒體文件。請(qǐng)注意,視頻文件不支持下載,調(diào)用該接口需http協(xié)議。 下載文件使用獲取圖片數(shù)據(jù),寫入新文件的方法。 http請(qǐng)求方式: GET http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID 參數(shù)說明
? 下載圖片 代碼實(shí)現(xiàn) 返回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} ? 下載語音 代碼實(shí)現(xiàn) 返回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} 可以看出,MP3的語音格式被壓縮成AMR格式了。 ? 下載縮略圖 實(shí)現(xiàn)代碼 返回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} ?至于回復(fù)相應(yīng)的消息,就是利用消息接口或者客服接口,構(gòu)造成相應(yīng)的消息就行了,和構(gòu)造文本,圖文消息的方法是一樣的。 更多微信公眾平臺(tái)開發(fā) ?上傳下載多媒體文件?相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)! |

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
