微信模板消息开发代码是什么 php文件如下

admin2周前未分类5

  微信模板消息开发代码是什么?如何开发微信模板消息?

  以下是小编整理的微信模板消息开发代码了,如果你要实现微信模板消息开发,那么以下的微信模板消息开发代码一定会有用。

  微信模板消息开发怎么做?

  我们需要将一些行为的进展消息推送给微信用户。除了短信,发送微信模板消息也是不错的选择。模板消息免费、精准到达、而且可以引导用户回到网站上来。但它有两个前提条件。

  1、必须开通了微信支付功能,你才能选择模板。

  2、被推送的微信用户必须关注微信公众号,而且你也拿到了他的openid。

  该方法基于thinkphp实现实现,具体OrderPush.class.php文件如下:

  微信代码如下:

  namespace Org\Weixin;

  class OrderPush

  {

  protected $appid;

  protected $secrect;

  protected $accessToken;

  function __construct($appid, $secrect)

  {

  $this->appid=$appid;

  $this->secrect=$secrect;

  $this->accessToken=$this->getToken($appid, $secrect);

  }

  function request_post($url='', $param='')

  {

  if (empty($url) || empty($param)) {

  return false;

  }

  $postUrl=$url;

  $curlPost=$param;

  $ch=curl_init(); //初始化curl

  curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页

  curl_setopt($ch, CURLOPT_HEADER, 0); //设置header

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上

  curl_setopt($ch, CURLOPT_POST, 1); //post提交方式

  curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);

  $data=curl_exec($ch); //运行curl

  curl_close($ch);

  return $data;

  }

  function request_get($url='')

  {

  if (empty($url)) {

  return false;

  }

  $ch=curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $data=curl_exec($ch);

  curl_close($ch);

  return $data;

  }

  protected function getToken($appid, $appsecret)

  {

  if (S($appid)) {

  $access_token=S($appid);

  } else {

  $url=""

  . $appid . "&secret=" . $appsecret;

  $token=$this->request_get($url);

  $token=json_decode(stripslashes($token));

  $arr=json_decode(json_encode($token), true);

  $access_token=$arr['access_token'];

  S($appid, $access_token, 720);

  }

  return $access_token;

  }

  public function doSend($touser, $template_id, $url, $data, $topcolor='#7B68EE')

  {

  $template=array(

  'touser'=> $touser,

  'template_id'=> $template_id,

  'url'=> $url,

  'topcolor'=> $topcolor,

  'data'=> $data

  );

  $json_template=json_encode($template);

  $url="" .

  $this->accessToken;

  $dataRes=$this->request_post($url, urldecode($json_template));

  if ($dataRes['errcode']==0) {

  return true;

  } else {

  return false;

  }

  }

  }

  好了,微信模板消息开发代码已经整理出来了,各位微信用户们,希望以上的微信模板消息开发代码可以帮您成功的开发出微信模板消息哦。

标签: okid消息