微信开发案例与开发注意

浏览:933 发布日期:2017/03/17 分类:用法示例 关键字: 微信推送 微信主动推送
微信开发 主动推送实现
前言调试需要在线完成,请将代码上传至FTP上测试。

1. 引用文件 主程序
include_once("Wechat_Client.php");
class WeixinController extends Controller {
public function tuisong () {
// 1.1创建对象
$weixin = new Wechat_Client();
// 1.2.获取token
$access_token = $weixin->getAccessToken(); //获取微信token
// 1.3.发送给用户的微信ID(openid 最少是2个,最多10000个)
$openid = array("oZb50wbMlLNLcuyalgZKK_ueDWA8","oZb50wTKx1rOSvWfobts121AlAlM");
$openid = json_encode($openid); //将数组转化成json
// 注意:发送中文直接用php内部方法转json会出现编码问题,
// 可以直接发送json的中文格式内容体无编码问题。
// 1.4.组装发送体[这是个文本格式]
$postJson = '{
"touser":'.$openid.',
"msgtype": "text",
"text": { "content": "测试测试"}
}';
// 1.5.推送到微信服务器[注意要发送的文本类型->请查官方阅文档]
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$access_token;
$res = $this->httpPost($url, $postJson);
//================================================================
/* 成功后返回的结果
{
"errcode":0,
"errmsg":"send job submission success",
"msg_id":3147483651
}
*/

}
}

2.微信 类文件

<?php
/**
* 微信公众平台PHP-SDK, 官方API部分
*/

class Wechat_Client
{
// 测试账号
public $AppId = "申请您的 AppId";
public $AppSecret = "申请您的 AppSecret";

public $Token = "";
public $Refresh_Token = "";
public $Openid = "";

public function __construct(){}

public function request($callback="",$state="STATE",$scope="snsapi_userinfo"){
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->AppId}&redirect_uri={$callback}&response_type=code&scope={$scope}&state={$state}#wechat_redirect";
header("Location:{$url}");
}

public function callback(){

$code = $_GET["code"];
if(empty($code)){
exit("empty code");
}
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->AppId}&secret={$this->AppSecret}&code={$code}&grant_type=authorization_code";

$result = $this->get($url);

$msg = json_decode($result,true);

$this->Token = $msg["access_token"];
$this->Refresh_Token = $msg["refresh_token"];
$this->Openid = $msg["openid"];

$data["openid"] = $msg["openid"];
return $data;
}

public function getUser(){
//$access_token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$this->Token}&openid={$this->Openid}&lang=zh_CN";
$result = $this->get($url);
return json_decode($result,true);
}


public function getAccessToken() {
$data = json_decode($this->get_php_file(dirname(__FILE__)."/userinfo_token.php"));
if ($data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->AppId."&secret=".$this->AppSecret;
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$this->set_php_file(dirname(__FILE__)."/userinfo_token.php", json_encode($data));
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}

private function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
}
private function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "<?php exit();?>" . $content);
fclose($fp);
}


public function get($url)
{
$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);
curl_close($curl);

return $res;
}

}

3. 依存在文件 access_token.php[格式可以自定义]
<?php exit();?>{"access_token":"","expire_time":0}

4.完成
评论( 相关
后面还有条评论,点击查看>>