public static function sendPostRequest($url, $postParameters, $responseType = 'raw') { $snoopy = new Snoopy(); $snoopy->accept = 'application/json'; $snoopy->set_submit_normal(); $snoopy->submit($url, $postParameters); $response = null; switch ($responseType) { case 'json': $response = json_decode($snoopy->results); break; case 'raw': $response = $snoopy->results; break; default: $response = $snoopy->results; } // on failure, throw an exception if ($snoopy->status != 200) { if ($responseType == 'raw') { throw new HttpException($response, $snoopy->status); } elseif ($responseType == 'json') { if (isset($response->field_errors) && !empty($response->field_errors)) { $errorMsg = "["; $i = 0; foreach ($response->field_errors as $field_error) { $errorMsg .= $i != 0 ? ' | ' . $field_error : $field_error; ++$i; } $errorMsg .= "]"; throw new HttpException($response->message . ' => ' . $errorMsg, $snoopy->status); } else { throw new HttpException($response, $snoopy->status); } } } return $response; }
/** * 发送预览图文消息 * @param string $account 账户名称 * @param string $title 标题 * @param string $summary 摘要 * @param string $content 内容 * @param string $photoid 素材库里的图片id(可通过uploadFile上传后获取) * @param string $srcurl 原文链接 * @return json */ public function sendPreview($account, $title, $summary, $content, $photoid, $srcurl = '') { $send_snoopy = new Snoopy(); $submit = "https://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 = 'https://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); $post['token'] = $this->_token; $send_snoopy->submit($submit, $post); $tmp = $send_snoopy->results; $this->log('step2:' . $tmp); $json = json_decode($tmp, true); return $json; }
/** * 发送图文消息 * @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; }