Heim > Artikel > Backend-Entwicklung > 微信公众平台模拟登陆上传素材(nodejs转php)
微信公众平台模拟登陆上传素材(求助nodejs转php)
MPWechat.prototype.uploadMedia = function(medianame,reqData,callback){<br> tools.log('uploadMedia start ...');<br> var boundary = 'tvmin';<br> var max = 9007199254740992;<br> var dec = Math.random() * max;<br> var hex = boundary + dec.toString(36);<br> var mimes = {<br> //image<br> '.bmp': 'image/bmp',<br> '.png': 'image/png',<br> '.gif': 'image/gif',<br> '.jpg': 'image/jpeg',<br> '.jpeg': 'image/jpeg',<br> /oice<br> '.mp3': 'audio/mp3',<br> '.wav': 'audio/x-wav',<br> '.amr': 'audior',<br> '.wma': 'audio/x-ms-wma',<br> /ideo<br> '.mp4': 'video/mp4',<br> '.rm': 'video/rm',<br> '.rmvb': 'videond.rn-realvideo',<br> '.wmv': 'video/x-ms-wmv',<br> '.avi': 'video/x-msvideo',<br> '.mpg': 'video/mpeg',<br> '.mpeg': 'video/mpeg'<br> };<br> var ext = path.extname(medianame).toLowerCase();<br> var mime = mimes[ext];<br> var boundaryKey = '----WebKitFormBoundary' + hex;<br> var payload = '\r\n\r\n--' + boundaryKey + '\r\n'<br> + 'Content-Disposition: form-data; name="uploadfile"; filename="'+ medianame +'"\r\n'<br> + 'Content-Type: '+ mime +'\r\n\r\n';<br> var enddata = '\r\n--' + boundaryKey + '\r\n'<br> + 'Content-Disposition: form-data; name="formId"\r\n\r\n'<br> + '--' + boundaryKey + '--';<br> var contentLength = Buffer.byteLength(payload,'utf8') + reqData.length + Buffer.byteLength(enddata,'utf8');<br> var options = {<br> host: host,<br> port: 443,<br> method: 'POST',<br> path: '/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=0&token='+ this.token +'&t=iframe-uploadfile&lang=zh_CN&formId=null',<br> headers: {<br> 'Content-Type': 'multipart/form-data; boundary='+boundaryKey,<br> 'Content-Length': contentLength,<br> 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31',<br> 'Cookie': this.cookie,<br> 'Referer': 'https://'+ host +'/cgi-bin/indexpage?token='+ this.token +'&lang=zh_CN&t=wxm-upload&lang=zh_CN&type=0&fromId=file_from_1341151893625'<br> }<br> };<br> var req = https.request(options, function(response) {<br> var statusCode = response.statusCode;<br>// console.log('STATUS: ' + statusCode,options);<br> response.setEncoding('utf8');<br> var data = '';<br> response.on('data', function(chunk) {<br> data += chunk;<br> }).on('end',function(){<br> callback(data.match(/formId, '(\d+)'/)[1]);<br> });<br> });<br> req.write(payload,'utf8');<br> req.write(reqData,'binary');<br> req.write(enddata,'utf8');<br> req.end();<br><br> req.on('error', function(e) {<div class="clear"> </div>