/** * 上传附件(图片/音频/视频) * @param string $filepath 本地文件地址 * @param int $type 文件类型: 2:图片 3:音频 4:视频 */ public function uploadFile($filepath, $type = 2) { $send_snoopy = new Snoopy(); $send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-upload&lang=zh_CN&type=2&formId=1"; $t = time() . strval(mt_rand(100, 999)); $post = array('formId' => ''); $postfile = array('uploadfile' => $filepath); $send_snoopy->rawheaders['Cookie'] = $this->cookie; $send_snoopy->set_submit_multipart(); $submit = "http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type={$type}&token=" . $this->_token . "&t=iframe-uploadfile&lang=zh_CN&formId=\tfile_from_" . $t; $send_snoopy->submit($submit, $post, $postfile); $tmp = $send_snoopy->results; $this->log('upload:' . $tmp); preg_match("/formId,.*?\\'(\\d+)\\'/", $tmp, $matches); if (isset($matches[1])) { return $matches[1]; } return false; }
/** * 上传附件(图片/音频/视频) * @param string $username 用户ticket_id名称 * @param string $filepath 本地文件地址 * @param int $type 文件类型: 2:图片 3:音频 4:视频 */ public function uploadFile($filepath, $type = 2) { $send_snoopy = new Snoopy(); $send_snoopy->referer = "https://mp.weixin.qq.com/cgi-bin/filepage?type=2&begin=0&count=10&t=media/list&token=" . $this->_token . "&lang=zh_CN"; //$t = time().strval(mt_rand(100,999)); $post['Filename'] = ''; $post['folder'] = '/cgi-bin/uploads'; $postfile = array('file' => $filepath); $send_snoopy->rawheaders['Cookie'] = $this->cookie; $send_snoopy->set_submit_multipart(); $submit = "https://mp.weixin.qq.com/cgi-bin/filetransfer?action=upload_material&f=json&ticket_id=" . $this->_account . "&ticket=" . $this->getticket() . "&token=" . $this->_token . "&lang=zh_CN"; $send_snoopy->submit($submit, $post, $postfile); $tmp = $send_snoopy->results; $this->log('upload:' . $tmp); $ret = json_decode($tmp, 1); $fileid = $ret['content']; return $fileid; }
<?php exit; // Improvements could be made such that instead of base 64 encoding file content as a regular post string // You can use Snoopy's file upload (MIME encoding as a regular browser) // Needs some testing and analysis ... sample code is give below $url = "http://www.domain.tld/pfad/zum/formular.php"; include "Snoopy.class.php"; $snoopy = new Snoopy(); // Multi-Part aktivieren, sonst können keine Dateien übertragen werden $snoopy->set_submit_multipart(); $postVars = array(); $postVars['name'] = 'Snoopy'; $postFiles = array(); $postFiles['userfile'] = dirname(__FILE__) . '/bla.pdf'; $snoopy->submit($url, $postVars, $postFiles); $body = $snoopy->results; // Further all the file serving is actually done by PHP and not apache // this can be fixed by using Apache module ‘mod_xsendfile’. ( http://www.jasny.net/articles/how-i-php-x-sendfile/ ) or https://tn123.org/mod_xsendfile/ // When serving files like this - if you want to send meta data along you can use custom http header strings header("X-Sendfile: {$somefile}"); header("Content-type: application/octet-stream"); header('Content-Disposition: attachment; filename="' . $filename . '"');
/** * 发送图文消息 * @param string $account 账户名称 * @param string $title 标题 * @param string $summary 摘要 * @param string $content 内容 * @param string $pic 图片 * @param string $srcurl 原文链接 * @return json */ public function sendNews($account, $title, $summary, $content, $pic, $srcurl = '') { $send_snoopy = new Snoopy(); $send_snoopy->referer = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-upload&lang=zh_CN&type=2&formId=1"; $post = array('formId' => ''); $postfile = array('uploadfile' => $pic); $send_snoopy->rawheaders['Cookie'] = $this->cookie; $send_snoopy->set_submit_multipart(); $submit = "http://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=2&t=iframe-uploadfile&lang=zh_CN&formId=1"; $send_snoopy->submit($submit, $post, $postfile); $tmp = $send_snoopy->results; $this->log($tmp); preg_match("/formId,.*?\\'(\\d+)\\'/", $tmp, $matches); if (isset($matches[1])) { $photoid = $matches[1]; $send_snoopy = new Snoopy(); $submit = "http://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=preview&t=ajax-appmsg-preview"; $send_snoopy->set_submit_normal(); $send_snoopy->rawheaders['Cookie'] = $this->cookie; $send_snoopy->referer = 'http://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=edit&t=wxm-appmsgs-edit-new&type=10&subtype=3&lang=zh_CN'; $post = array('AppMsgId' => '', 'ajax' => 1, 'content0' => $content, 'count' => 1, 'digest0' => $summary, 'error' => 'false', 'fileid0' => $photoid, 'preusername' => $account, 'sourceurl0' => $srcurl, 'title0' => $title); $send_snoopy->submit($submit, $post); $tmp = $send_snoopy->results; $this->log($tmp); $json = json_decode($tmp, true); return $json; } return false; }