You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
5
wxtmp/Message.php
Executable file
5
wxtmp/Message.php
Executable file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
public class Message(){
|
||||
$appid = '';
|
||||
$appsecret = '';
|
||||
}
|
75
wxtmp/index.php
Executable file
75
wxtmp/index.php
Executable file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
$appid = 'wx2b29f5846b8d00fa';
|
||||
$appsecret = 'd8b93b7e2b175b904c435cb493b66ff8' ;
|
||||
$tenpalate_id = 'ObkTv-txyqsv8Toie4HC8PnBFJmwygw9zPJbx8h0FcM';
|
||||
var_dump(send_notice($appid,$appsecret,$tenpalate_id ));
|
||||
/**
|
||||
* 发送模板消息
|
||||
*/
|
||||
function send_notice($appid,$appsecret,$tenpalate_id ){
|
||||
//获取access_token
|
||||
if (isset($_COOKIE['access_token'])){
|
||||
$access_token = $_COOKIE['access_token'];
|
||||
}else{
|
||||
$json_token=curl_post('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret);
|
||||
$access_token=json_decode($json_token,true);
|
||||
$access_token=$access_token['access_token'];
|
||||
setcookie('access_token',$access_token,7200);
|
||||
}
|
||||
//模板消息
|
||||
$json_template = json_tempalte('objYQuHNqKTi2sjSTzLTc7C7PjDo',$tenpalate_id,'http://t.ect99.com');
|
||||
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
|
||||
|
||||
$res=curl_post($url,urldecode($json_template));
|
||||
$res = json_decode($res);
|
||||
var_dump($res);
|
||||
if ($res['errcode']==0){
|
||||
return '发送成功';
|
||||
}else{
|
||||
return '发送失败';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将模板消息json格式化
|
||||
*/
|
||||
function json_tempalte($openid,$tenpalate_id ,$url){
|
||||
//模板消息
|
||||
$template=array(
|
||||
'touser'=>$openid, //用户openid
|
||||
'template_id'=>$tenpalate_id, //在公众号下配置的模板id
|
||||
'url'=>$url, //点击模板消息会跳转的链接
|
||||
'topcolor'=>"#7B68EE",
|
||||
'data'=>array(
|
||||
'first'=>array('value'=>urlencode("您的活动已通过"),'color'=>"#FF0000"),
|
||||
'keyword1'=>array('value'=>urlencode('测试文章标题'),'color'=>'#FF0000'), //keyword需要与配置的模板消息对应
|
||||
'keyword2'=>array('value'=>urlencode(date("Y-m-d H:i:s")),'color'=>'#FF0000'),
|
||||
'keyword3'=>array('value'=>urlencode('测试发布人'),'color'=>'#FF0000'),
|
||||
'keyword4'=>array('value'=>urlencode('测试状态'),'color'=>'#FF0000'),
|
||||
'remark' =>array('value'=>urlencode('备注:这是测试'),'color'=>'#FF0000'), )
|
||||
);
|
||||
$json_template=json_encode($template);
|
||||
return $json_template;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
* curl请求
|
||||
*/
|
||||
function curl_post($url , $data=array()){
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
||||
// POST数据
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
// 把post的变量加上
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
$output = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return $output;
|
||||
}
|
28
wxtmp/pptWX.php
Executable file
28
wxtmp/pptWX.php
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
require 'pptWXBase.php';
|
||||
class pptWX extends pptWXBase
|
||||
{
|
||||
public function index(){
|
||||
$abc='11111';
|
||||
return $abc;
|
||||
}
|
||||
public function getList(){
|
||||
$abc=[1,2,3,4,5];
|
||||
return $abc;
|
||||
}
|
||||
|
||||
}
|
||||
//获取数据,通过路由信息,如***.com/passport/wx.php/pptWX/getList,即调用pptWX类,getList方法
|
||||
function getData(){
|
||||
$uri = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
|
||||
foreach ($uri as $k=>$v) {
|
||||
if('pptWX' == $v){
|
||||
$class = $uri[$k];
|
||||
$object = new $class();
|
||||
$action = $uri[($k+1)];
|
||||
return $object->$action();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
214
wxtmp/pptWXBase.php
Executable file
214
wxtmp/pptWXBase.php
Executable file
@ -0,0 +1,214 @@
|
||||
<?php
|
||||
require '../vendor/wechat/vendor/autoload.php';
|
||||
class pptWXBase
|
||||
{
|
||||
protected $openid;
|
||||
public function __construct(){
|
||||
session_start();
|
||||
//$_SESSION['openid'] = null;
|
||||
if(!isset($_SESSION['openid'])){
|
||||
$openid = $this->getHeaderValue('openid');
|
||||
if(empty($openid)){
|
||||
//去登录
|
||||
$code = isset($_GET['code']) ? $_GET['code'] : '';
|
||||
$this->wxLogin($code);
|
||||
exit();
|
||||
}else{
|
||||
//已经有openid了,不用登录
|
||||
$_SESSION['openid'] = $openid;
|
||||
}
|
||||
}
|
||||
$this->openid = $_SESSION['openid'];
|
||||
}
|
||||
/**
|
||||
* 获取请求头
|
||||
* @param [string] $keyName [头名]
|
||||
* @return [string] [头值]
|
||||
*/
|
||||
public function getHeaderValue($keyName){
|
||||
$headerValue = '';
|
||||
if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
|
||||
$headerValue = $result[$keyName];
|
||||
} else {
|
||||
foreach ($_SERVER as $key => $val) {
|
||||
if (0 === strpos($key, 'HTTP_')) {
|
||||
$key = str_replace('_', '-', strtolower(substr($key, 5)));
|
||||
if($key == $keyName){
|
||||
$headerValue = $val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $headerValue;
|
||||
}
|
||||
/**
|
||||
* 调用微信登录
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function wxLogin($code){
|
||||
//登录过程
|
||||
if(!$code){
|
||||
//调取授权
|
||||
$this->getAuth();
|
||||
}else{
|
||||
//获取openid,同时获取用户信息
|
||||
$authInfo = $this->getOpenid($code,true);
|
||||
if(isset($authInfo['openid'])){
|
||||
$_SESSION['openid'] = $authInfo['openid'];
|
||||
$this->openid = $_SESSION['openid'];
|
||||
echo "<script>document.location.reload()</script>";exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
*第一步:用户同意授权,获取code
|
||||
*getAuth();
|
||||
*第二步:通过code换取网页授权access_token
|
||||
*getOpenid($code,$type=false){
|
||||
*第三步:拉取用户信息(需scope为 snsapi_userinfo)
|
||||
*微信用户信息
|
||||
*第一种,直接在第二步获取openid时获取
|
||||
*getOpenid($code,$type=true);
|
||||
*第二种 通过用户授权accress_token获取
|
||||
*getUserInfo($userAuthAccessToken, $openid);
|
||||
*第三种 通过公众号accressToken获取
|
||||
*getUserInfoByToken($accessToken, $openid)
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* 获取用户授权CODE,回调为当前地址,需要判断Code
|
||||
*
|
||||
*/
|
||||
public function getAuth(){
|
||||
\WeChat\Core\User::code(); # 重载微信授权
|
||||
}
|
||||
/**
|
||||
* 通过code获取openid,access_token或用户信息
|
||||
* @param [string] $code [用户授权code]
|
||||
* @param [string] $type [true:同时获取用户信息 | false:只获取用户openid]
|
||||
* @return [string] [openid]
|
||||
*/
|
||||
public function getOpenid($code, $type=false){
|
||||
return \WeChat\Core\User::openid($code, $type);
|
||||
}
|
||||
/**
|
||||
* 通过用户授权access_token获取用户信息
|
||||
* @param [string] $userAuthAaccessToken [用户授权access_token]
|
||||
* @param [string] $openid [用户openid]
|
||||
* @return [array] []
|
||||
*/
|
||||
public function getUserInfo($userAuthAccessToken, $openid){
|
||||
return \WeChat\Core\User::userInfo($userAuthAccessToken, $openid);
|
||||
}
|
||||
/**
|
||||
* 通过公众号accessToken获取用户信息
|
||||
* @param [string] $accessToken [公众号access_token]
|
||||
* @param [string] $openid [用户openid]
|
||||
* @return [array] []
|
||||
*/
|
||||
public function getUserInfoByToken($openid){
|
||||
return \WeChat\Core\User::newUserInfo($openid);
|
||||
}
|
||||
/**
|
||||
* 获取公众号下所有模板信息
|
||||
* @return [array] [模板列表]
|
||||
*/
|
||||
public function getAllTemplateMsg(){
|
||||
$accessToken = $this->getAccessToken();
|
||||
return \WeChat\Core\Template::gain($accessToken);
|
||||
}
|
||||
/**
|
||||
* 获取用户accessToken
|
||||
* @return [string] accessToken
|
||||
*/
|
||||
public function getAccessToken(){
|
||||
$accessToken = \WeChat\Core\Token::gain(); # 获取微信access_token
|
||||
$accessToken = $accessToken['access_token'];
|
||||
return $accessToken;
|
||||
}
|
||||
/**
|
||||
* 发送模板消息
|
||||
* @param [string] $templateId [模板ID]
|
||||
* @param [string] $openid [用户openid]
|
||||
* @param [array] $data [消息数据]
|
||||
* @param [string] $url [点击跳转地址]
|
||||
* @param string $color [标题颜色]
|
||||
* @return [array] 消息是否成功发送
|
||||
* errcode: 0成功,其他失败
|
||||
* errmsg: 错误信息,ok成功,其他失败
|
||||
* msgid: 消息id
|
||||
*
|
||||
*/
|
||||
public function sendTemplateMsg($templateId, $openid, $data, $url, $color = '#7B68EE'){
|
||||
//\WeChat\Core\User::code($appID); # 重载微信授权
|
||||
$accessToken = $this->getAccessToken();
|
||||
return \WeChat\Core\Send::push($accessToken, $templateId, $openid, $data, $url, $color);
|
||||
}
|
||||
}
|
||||
// $templateId = 'ObkTv-txyqsv8Toie4HC8PnBFJmwygw9zPJbx8h0FcM';
|
||||
// $openid = 'objYQuHNqKTi2sjSTzLTc7C7PjDo';
|
||||
// $url = 'http://t.ect99.com';
|
||||
// //模板消息
|
||||
// $data = [
|
||||
// 'first'=>['value'=>'您的活动已通过','color'=>"#FF0000"],
|
||||
// 'keyword1'=>['value'=>'测试文章标题'], //keyword需要与配置的模板消息对应
|
||||
// 'keyword2'=>['value'=>date("Y-m-d H:i:s")],
|
||||
// 'keyword3'=>['value'=>'测试发布人'],
|
||||
// 'keyword4'=>['value'=>'测试状态'],
|
||||
// 'remark' =>['value'=>'备注:这是测试']
|
||||
// ];
|
||||
// var_dump(pptWX->sendTemplateMsg($templateId, $openid, $data, $url));
|
||||
// //登录过程
|
||||
// $code = isset($_GET['code']) ? $_GET['code'] : '';
|
||||
// if(!$code){
|
||||
// //调取授权
|
||||
// pptWXBase::getAuth();
|
||||
// }else{
|
||||
// //获取授权信息,只获取openid
|
||||
// //$authInfo = pptWXBase::getOpenid($code);
|
||||
// // 返回代码示例
|
||||
// // array(5) {
|
||||
// // ["access_token"]=>
|
||||
// // string(89) "22_HQ4kM7_gbUZ-fGP1TnFLeh-wxk-jWyjc_PzrbYpICXyuGCNKWEi_Yz5_qnStDGP56I8OD4xJvNRVS98U1TC6Zw"
|
||||
// // ["expires_in"]=>
|
||||
// // int(7200)
|
||||
// // ["refresh_token"]=>
|
||||
// // string(89) "22_bdxpWwS0wADfSNO1y6Sq7txoWBK3hG4x3vvx_WOe-sovDig-yWZ43MsBtzJlY2svcxmlvLxKwBughKbJL14b8A"
|
||||
// // ["openid"]=>
|
||||
// // string(28) "objYQuHNqKTi2sjSTzLTc7C7PjDo"
|
||||
// // ["scope"]=>
|
||||
// // string(15) "snsapi_userinfo"
|
||||
// // }
|
||||
// //获取用户信息
|
||||
// //通过授权token,获取用户信息
|
||||
// //$userInfo = pptWXBase::getUserInfo($authInfo['access_token'], $authInfo['openid']);
|
||||
// //获取openid,同时获取用户信息
|
||||
// $authInfo = pptWXBase::getOpenid($code,true);
|
||||
// // 返回代码示例
|
||||
// // array(9) {
|
||||
// // ["openid"]=>
|
||||
// // string(28) "objYQuHNqKTi2sjSTzLTc7C7PjDo"
|
||||
// // ["nickname"]=>
|
||||
// // string(12) "网事悠悠"
|
||||
// // ["sex"]=>
|
||||
// // int(1)
|
||||
// // ["language"]=>
|
||||
// // string(5) "zh_CN"
|
||||
// // ["city"]=>
|
||||
// // string(6) "枣庄"
|
||||
// // ["province"]=>
|
||||
// // string(6) "山东"
|
||||
// // ["country"]=>
|
||||
// // string(6) "中国"
|
||||
// // ["headimgurl"]=>
|
||||
// // string(130) "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJW7ghRsPm6dJReYJ97lUMicgjQLVdSaPr3SKBZ29riaZ3KOFaw1nmKyt4LtibvN2FkzFTTESgetm9QQ/132"
|
||||
// // ["privilege"]=>
|
||||
// // array(0) {
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// var_dump($authInfo);
|
||||
// }
|
6
wxtmp/url.php
Executable file
6
wxtmp/url.php
Executable file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
$current_url = 'http://t.ect99.com/wxtmp/wx.php';
|
||||
$appid = 'wx2b29f5846b8d00fa';
|
||||
|
||||
|
||||
header('Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlencode($current_url).'&response_type=code&scope=snsapi_base&state=123#wechat_redirect');
|
15
wxtmp/wx.php
Executable file
15
wxtmp/wx.php
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
require 'pptWX.php';
|
||||
$data = getData();
|
||||
?>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<?php foreach($data as $item): ?>
|
||||
<?=$item?>
|
||||
<?php endforeach ?>
|
||||
aaaaaaaaaaaaaaaaaaaaa
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user