12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- /**
- * 模板消息
- * Created by 上海风车教育科技有限公司.
- * User: 刘红伟
- * Date: 15-12-10
- * Email: 454303753@qq.com
- * File:wechattemplateapi.php
- */
- class wechattemplateapi{
- private $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
- private $token;
- private $template_id;
- private $parameter=array();
- private $tem_type=1;
- public function __construct($token,$data=array(),$tem_type=1){
- $this->token=$token;
- $this->tem_type=$tem_type;
- $this->parameter=$data;
- }
- //选择模板
- private function create_tem(){
- switch($this->tem_type){
- case 1:
- $this->template_id='oSGXMc3RxDr92mPBYV80Z7yBp0LZ3ulMgY4sdaNn0Sk';
- $result= $this->test_template();
- break;
- }
- return $result;
- }
- //充值模板内容
- private function test_template(){
- //写模板内容
- return json_encode(array(
- 'touser'=>$this->parameter['openId'],
- 'template_id'=>$this->template_id,
- 'url'=> $this->parameter['url'],
- 'topcolor'=>'#000000',
- 'data'=>array(
- 'name'=>array('value'=>$this->parameter['name'],'color'=>'#000000')
- )
- )
- );
- }
- //发送模板消息
- public function send(){
- $this->url=$this->url.$this->token;
- $http = wx_http::factory($this->url, wx_http::TYPE_CURL);
- $json = $http->post($this->url, $this->create_tem(), array(
- 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)',
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: '. strlen($this->create_tem())
- ));
- $result =json_decode($json,true);
- if(!empty($result) && $result['errcode']==0 and $result['errmsg']=='ok'){
- return true;
- }else{
- return false;
- }
- }
- }
|