WeChat public platform Simulation login upload material (help Nodejs to PHP)

Source: Internet
Author: User
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=uplo Admaterial&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/53 7.31 ', ' Cookie ': This.cookie, ' Referer ': ' https://' + host + '/cgi-bin/indexpage?token= ' + This.token + ' &LANG=ZH_CN&Amp;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 (' S        Tatus: ' + 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); });};


The above is the Nodejs version upload code

The source author says just turn the file into byte.
and file names, call this function together.
There are only two parameters

The image format must be in the following format: BMP, PNG, JPEG, JPG, GIF
The speech format must be in the following format: MP3, WMA, WAV, AMR
The video format must be in the following format: RM, RMVB, WMV, AVI, MPG, MPEG, MP4

I tried to rewrite it for PHP, but it was a few days.
The interface uploads 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 '; Disguise source page Address $submit = ' https://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=0&token= '; $ Submit. = $cooken. ' &t=iframe-uploadfile&lang=zh_cn&&formid=null ';//Picture submission Address $ext = Strtolower (Trim (substr ($        File, '. '), 1, 10)); if ($ext = = ") return" Sorry, the file name is wrong! "; $mime _types = Array (//image supports the picture ' bmp ' = ' image/bmp ', ' png ' = ' image/png ', ' gif ' = ' image/gif ', ' jpg ' = ' I Mage/jpeg ', ' jpeg ' = ' image/jpeg ',//oice supports the Voice ' mp3 ' = ' audio/mp3 ', ' wav ' = ' audio/x-wav ', ' amr ' = ' audior ' , ' wma ' = ' audio/x-ms-wma ',//ideo supported video ' mp4 ' = ' video/mp4 ', ' rm ' = ' video/rm ', ' rmvb ' and ' = ' 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 (), if (Empty ($title)) {$medianame = Self::randstr (10). $ext;//Set file name}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 ("/login timeout.)Re-login/", $results)) {//echo" login timeout, send error! (Set_time_limit); sleep (2); Self::login (); Self::qsend ($content, $type);} $result = Json_decode ($results, true); if (Preg_match ("/Upload succeeded/", $results) {echo "Congratulations, the file upload was successful!) ";} else if (Preg_match ("/Upload file Failed/", $results) {echo "Sorry, uploading the file failed! ";//echo var_dump ($_server); $_server[path_translated];} else {echo ' Mass failed! ";} return $result;} Public Function Randstr ($len =6) {$chars = ' ABDEFGHJKLMNPQRSTVWXYkfbd23456789 ';//characters to build the password from//a Bdefghjklmnpqrstvwxyabdefghijkmnpqrstvwxy23456789#%*mt_srand (Double) Microtime () *1000000*getmypid ()); Seed the random number generater (must is done) $password = '; while (strlen ($password) < $len) $password. =substr ($chars, (Mt_rand ()%strlen ($chars)), 1); return $password; }


This is part of the code I wrote with curl_setopt.
Ask for a big help rewriting


Reply to discussion (solution)

Error? What are you suggesting? Put out the wrong message.

Error? What are you suggesting? Put out the wrong message.

Sorry, uploading the file failed!

Return this if the login information is correct.


Ask the Great God!

Landlord I want to ask, your this analog login code is how to write, I use Snoopy analog login, pass to Http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN four value username,pwd,f, Imgcode always return login unsuccessful, what's going on? Do you want to pass some cookies as well?

Landlord I want to ask, your this analog login code is how to write, I use Snoopy analog login, pass to Http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN four value username,pwd,f, Imgcode always return login unsuccessful, what's going on? Do you want to pass some cookies as well?
Snoopy is no use, now the platform updated these simulations are useless, white whole!
First look at the interface is not good if you apply for a good application, directly with the interface
No, just once again strong, rape the public platform!

$url = "https://mp.weixin.qq.com/cgi-bin/filetransfer?action=upload_material&f=json&ticket_id={$username    }&ticket={$ticket}&token={$this->token}&lang=zh_cn "; $this->send_data = array (' action ' = ' upload_material ', ' ticket_id ' = = $username, ' ticket ' = = $ticke T, ' token ' = = $this->token, ' ajax ' = 1, ' file ' = ' @ '. Str_replace ('/', ' \ \ ', $_server[' document_root ' ])." \\uploads\\file\\20140324104305420.jpg ", ' folder ' = '/cgi-bin/uploads ', ' Upload ' = ' Submit Query ', ' t ' =& gt; '    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. '                &AMP;LANG=ZH_CN '; return $this->curlpostfile ($url);;
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.