您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

微信公众平台模拟登陆上传素材(求助nodejs转php)

2024/2/26 23:19:51发布22次查看
mpwechat.prototype.uploadmedia = function(medianame,reqdata,callback){ tools.log('uploadmedia start ...'); var boundary = 'tvmin'; var max = 9007199254740992; var dec = math.random() * max; var hex = boundary + dec.tostring(36); var mimes = { //image '.bmp': 'image/bmp', '.png': 'image/png', '.gif': 'image/gif', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', /oice '.mp3': 'audio/mp3', '.wav': 'audio/x-wav', '.amr': 'audior', '.wma': 'audio/x-ms-wma', /ideo '.mp4': 'video/mp4', '.rm': 'video/rm', '.rmvb': 'videond.rn-realvideo', '.wmv': 'video/x-ms-wmv', '.avi': 'video/x-msvideo', '.mpg': 'video/mpeg', '.mpeg': 'video/mpeg' }; var ext = path.extname(medianame).tolowercase(); var mime = mimes[ext]; var boundarykey = '----webkitformboundary' + hex; var payload = '\r\n\r\n--' + boundarykey + '\r\n' + 'content-disposition: form-data; name=uploadfile; filename='+ medianame +'\r\n' + 'content-type: '+ mime +'\r\n\r\n'; var enddata = '\r\n--' + boundarykey + '\r\n' + 'content-disposition: form-data; name=formid\r\n\r\n' + '--' + boundarykey + '--'; var contentlength = buffer.bytelength(payload,'utf8') + reqdata.length + buffer.bytelength(enddata,'utf8'); var options = { host: host, port: 443, method: 'post', path: '/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=0&token='+ this.token +'&t=iframe-uploadfile&lang=zh_cn&formid=null', headers: { 'content-type': 'multipart/form-data; boundary='+boundarykey, 'content-length': contentlength, 'user-agent': 'mozilla/5.0 (windows nt 6.1) applewebkit/537.31 (khtml, like gecko) chrome/26.0.1410.43 safari/537.31', 'cookie': this.cookie, 'referer': 'https://'+ host +'/cgi-bin/indexpage?token='+ this.token +'&lang=zh_cn&t=wxm-upload&lang=zh_cn&type=0&fromid=file_from_1341151893625' } }; var req = https.request(options, function(response) { var statuscode = response.statuscode;// console.log('status: ' + statuscode,options); response.setencoding('utf8'); var data = ''; response.on('data', function(chunk) { data += chunk; }).on('end',function(){ callback(data.match(/formid, '(\d+)'/)[1]); }); }); req.write(payload,'utf8'); req.write(reqdata,'binary'); req.write(enddata,'utf8'); req.end(); req.on('error', function(e) { console.error(error:+e); });};
以上是nodejs版本上传代码
源作者说只要把文件变成 byte
和文件名,一起call这个function就可以了
就只有两个参数
图片格式必须为以下格式:bmp, png, jpeg, jpg, gif
语音格式必须为以下格式:mp3, wma, wav, amr
视频格式必须为以下格式:rm, rmvb, wmv, avi, mpg, mpeg, mp4
想改写成php的 但改写了几天老是出错
//接口上传 public function pictures($data,$file){ $cookie =$this->read('cookie.log'); $cooken = self::read2('cooken.log'); $https = new https; $referer = 'https://mp.weixin.qq.com/cgi-bin/indexpage?token='; $referer .= $cooken.'&lang=zh_cn&t=wxm-upload&lang=zh_cn&type=0&fromid=file_from_1341151893625'; //伪装来源页地址 $submit = 'https://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=0&token='; $submit .= $cooken.'&t=iframe-uploadfile&lang=zh_cn&&formid=null';//图片提交地址 $ext = strtolower(trim(substr(strrchr($file, '.'), 1, 10))); if($ext == '') return 对不起,文件名错误!; $mime_types = array ( //image 支持的图片 'bmp' => 'image/bmp', 'png' => 'image/png', 'gif' => 'image/gif', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', //oice 支持的语音 'mp3' => 'audio/mp3', 'wav' => 'audio/x-wav', 'amr' => 'audior', 'wma' => 'audio/x-ms-wma', //ideo 支持的视频 'mp4' => 'video/mp4', 'rm' => 'video/rm', 'rmvb' => 'videond.rn-realvideo', 'wmv' => 'video/x-ms-wmv', 'avi' => 'video/x-msvideo', 'mpg' => 'video/mpeg', 'mpeg' => 'video/mpeg' ); $mime = isset($mime_types[$ext]) ? $mime_types[$ext] : ''; $hexken = self::randstr(15); if (empty($title)){ $medianame = self::randstr(10).'.'.$ext;// 设置文件名 }else{ $medianame = $file; } $boundarykey = '----webkitformboundary'.$hexken; $enddata = \r\n\r\n--.$boundarykey.\r\n; $enddata .= 'content-disposition: form-data; name=uploadfile; filename='.$medianame.''; $enddata .= \r\ncontent-type: .$mime.\r\n\r\n.$data; $enddata .= \r\n--.$boundarykey.\r\n; $enddata .= 'content-disposition: form-data; name=formid'; $enddata .= \r\n\r\n--.$boundarykey.'--'; //echo $enddata; //file_put_contents(./data/f1.txt, date('c').\r\n.$content.\r\n,file_append); $result = $https->submit($submit,$enddata,$cookie,$referer); $results =$result['body']; file_put_contents(./data/data.txt, date('c').\r\n.$results.\r\n,file_append); echo $results; if(preg_match(/登录超时.*重新登录/, $results)){ //echo 登陆超时,发送出错!; set_time_limit(30); sleep(2); self::login(); self::qsend($content,$type); } //$result = json_decode($results,true); if(preg_match(/上传成功/, $results)){ echo 恭喜,文件上传成功!; }else if (preg_match(/上传文件失败/, $results)){ echo 对不起,上传文件失败!; //echo var_dump($_server);$_server[path_translated]; }else { echo 群发失败!; } return $result; } public function randstr($len=6){ $chars='abdefghjklmnpqrstvwxykfbd23456789'; // characters to build the password from //abdefghjklmnpqrstvwxyabdefghijkmnpqrstvwxy23456789#%* mt_srand((double)microtime()*1000000*getmypid()); // seed the random number generater (must be done) $password=''; while(strlen($password)token}&lang=zh_cn; $this->send_data = array( 'action' => 'upload_material', 'ticket_id' => $username, 'ticket' => $ticket, 'token' => $this->token, 'ajax' => 1, 'file' =>@.str_replace('/', '\\', $_server['document_root']).\\uploads\\file\\20140324104305420.jpg, 'folder' => '/cgi-bin/uploads', 'upload' => 'submit query', 't' =>'ajax-response', ); print_r($this->send_data); $this->getheader = 1; $this->referer = https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit&action=edit&type=10&ismul=0&isnew=1&lang=zh_cn&token={$this->token};//'https://mp.weixin.qq.com/cgi-bin/advanced?action=dev&t=advanced/dev&token='.$this->token.'&lang=zh_cn'; return $this->curlpostfile($url);;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product