|
|
<?php
|
|
|
|
|
|
class WeChatCommon {
|
|
|
class WeChatCommon
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 判断是否已关注公众号
|
|
|
*/
|
|
|
public function isAuth() {
|
|
|
public function isAuth()
|
|
|
{
|
|
|
$access_token = $this->getAccessToken();
|
|
|
$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".session('openid');
|
|
|
$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . session('openid');
|
|
|
$subscribe = json_decode(file_get_contents($subscribe_msg));
|
|
|
$gzxx = $subscribe->subscribe;
|
|
|
if($gzxx === 1){
|
|
|
if ($gzxx === 1) {
|
|
|
return true;
|
|
|
}else {
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -20,9 +22,10 @@ class WeChatCommon { |
|
|
/**
|
|
|
* 获取code
|
|
|
*/
|
|
|
public function code(){
|
|
|
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".config('AppID')."&redirect_uri=http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
|
|
|
header('location:'.$url);
|
|
|
public function code()
|
|
|
{
|
|
|
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . config('AppID') . "&redirect_uri=http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
|
|
|
header('location:' . $url);
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -30,8 +33,9 @@ class WeChatCommon { |
|
|
* @param $code
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function getOpenid($code){
|
|
|
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('AppID').'&secret='.config('AppSecret').'&code='.$code .'&grant_type=authorization_code';
|
|
|
public function getOpenid($code)
|
|
|
{
|
|
|
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . config('AppID') . '&secret=' . config('AppSecret') . '&code=' . $code . '&grant_type=authorization_code';
|
|
|
$ch = curl_init();
|
|
|
curl_setopt($ch, CURLOPT_URL, $get_token_url);
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
...
|
...
|
@@ -47,13 +51,13 @@ class WeChatCommon { |
|
|
* 获取access_token,全局缓存7200s
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function getAccessToken(){
|
|
|
$data=cache('Vendor/access_token');
|
|
|
if(!empty($data) && ((time()-$data['time']) < 7000)) {
|
|
|
public function getAccessToken()
|
|
|
{
|
|
|
$data = cache('Vendor/access_token');
|
|
|
if (!empty($data) && ((time() - $data['time']) < 7000)) {
|
|
|
return $data['access_token'];
|
|
|
}
|
|
|
else {
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('AppID')."&secret=".config('AppSecret')."";
|
|
|
} else {
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . config('AppID') . "&secret=" . config('AppSecret') . "";
|
|
|
$ch = curl_init();
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
...
|
...
|
@@ -63,10 +67,10 @@ class WeChatCommon { |
|
|
curl_close($ch);
|
|
|
$jsoninfo = json_decode($output, true);
|
|
|
$access_token = $jsoninfo["access_token"];
|
|
|
$time=time();
|
|
|
$data=array(
|
|
|
'access_token' => $access_token,
|
|
|
'time' => $time
|
|
|
$time = time();
|
|
|
$data = array(
|
|
|
'access_token' => $access_token,
|
|
|
'time' => $time
|
|
|
);
|
|
|
cache('Vendor/access_token', $data);
|
|
|
return $access_token;
|
...
|
...
|
@@ -77,8 +81,9 @@ class WeChatCommon { |
|
|
* 获取用户信息(头像、昵称等)
|
|
|
* @return array
|
|
|
*/
|
|
|
public function getUserInfo($openid){
|
|
|
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$this->getAccessToken().'&openid='.$openid.'&lang=zh_CN';
|
|
|
public function getUserInfo($info)
|
|
|
{
|
|
|
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $info['access_token'] . '&openid=' . $info['openid'] . '&lang=zh_CN';
|
|
|
$ch = curl_init();
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
...
|
...
|
@@ -96,16 +101,17 @@ class WeChatCommon { |
|
|
* @param $filename
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function curl_file_get_contents($url,$filename){
|
|
|
public function curl_file_get_contents($url, $filename)
|
|
|
{
|
|
|
$ch = curl_init();
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
|
|
|
curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
|
|
|
curl_setopt($ch, CURLOPT_REFERER, _REFERER_);
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
$r = curl_exec($ch);
|
|
|
curl_close($ch);
|
|
|
file_put_contents('data/upload/headimg/'.$filename, $r);
|
|
|
file_put_contents('data/upload/headimg/' . $filename, $r);
|
|
|
return $filename;
|
|
|
}
|
|
|
|
...
|
...
|
@@ -113,12 +119,13 @@ class WeChatCommon { |
|
|
* 获取js-sdkp票据,全局缓存7200s
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function get_jsapi_ticket(){
|
|
|
$ticket=cache('Vendor/ticket');
|
|
|
if(!empty($ticket) && ((time()-$ticket['time']) < 7000)) {
|
|
|
public function get_jsapi_ticket()
|
|
|
{
|
|
|
$ticket = cache('Vendor/ticket');
|
|
|
if (!empty($ticket) && ((time() - $ticket['time']) < 7000)) {
|
|
|
return $ticket['ticket'];
|
|
|
}else {
|
|
|
$url='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->getAccessToken().'&type=jsapi';
|
|
|
} else {
|
|
|
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $this->getAccessToken() . '&type=jsapi';
|
|
|
$ch = curl_init();
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
...
|
...
|
@@ -128,10 +135,10 @@ class WeChatCommon { |
|
|
curl_close($ch);
|
|
|
$json_obj = json_decode($res, true);
|
|
|
$jsapi_ticket = $json_obj['ticket'];
|
|
|
$time=time();
|
|
|
$data=array(
|
|
|
'ticket' => $jsapi_ticket,
|
|
|
'time' => $time
|
|
|
$time = time();
|
|
|
$data = array(
|
|
|
'ticket' => $jsapi_ticket,
|
|
|
'time' => $time
|
|
|
);
|
|
|
cache('Vendor/ticket', $data);
|
|
|
return $jsapi_ticket;
|
...
|
...
|
@@ -142,14 +149,15 @@ class WeChatCommon { |
|
|
* JS_SDK
|
|
|
* @return array
|
|
|
*/
|
|
|
public function js_sdk(){
|
|
|
$timestamp=time();
|
|
|
$string='jsapi_ticket='.$this->get_jsapi_ticket().'&noncestr=je9omv03bc5ryqz1×tamp='.$timestamp.'&url='.'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
|
|
|
$signature=sha1($string);
|
|
|
public function js_sdk()
|
|
|
{
|
|
|
$timestamp = time();
|
|
|
$string = 'jsapi_ticket=' . $this->get_jsapi_ticket() . '&noncestr=je9omv03bc5ryqz1×tamp=' . $timestamp . '&url=' . 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
|
|
$signature = sha1($string);
|
|
|
return array(
|
|
|
'appId' => config('AppId'),
|
|
|
'appId' => config('AppId'),
|
|
|
'timestamp' => $timestamp,
|
|
|
'nonceStr' => 'je9omv03bc5ryqz1',
|
|
|
'nonceStr' => 'je9omv03bc5ryqz1',
|
|
|
'signature' => $signature,
|
|
|
);
|
|
|
}
|
...
|
...
|
@@ -158,9 +166,10 @@ class WeChatCommon { |
|
|
* 创建菜单
|
|
|
* @return mixed|string
|
|
|
*/
|
|
|
public function creatMenu(){
|
|
|
public function creatMenu()
|
|
|
{
|
|
|
$ch = curl_init();
|
|
|
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->getAccessToken());
|
|
|
curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $this->getAccessToken());
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
...
|
...
|
@@ -181,7 +190,8 @@ class WeChatCommon { |
|
|
* 菜单内容JSON
|
|
|
* @return string
|
|
|
*/
|
|
|
public function menuItem(){
|
|
|
public function menuItem()
|
|
|
{
|
|
|
$data = '{
|
|
|
"button":[{
|
|
|
"type":"view",
|
...
|
...
|
@@ -205,12 +215,13 @@ class WeChatCommon { |
|
|
/**
|
|
|
* 上传永久素材
|
|
|
*/
|
|
|
public function eternalMaterial(){
|
|
|
public function eternalMaterial()
|
|
|
{
|
|
|
$file_info = array('filename' => '/public/images/soul_of_cinder.png', //国片相对于网站根目录的路径
|
|
|
'content-type' => 'image/jpg/png', //文件类型
|
|
|
'filelength' => '71' //图文大小
|
|
|
);
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->getAccessToken()."&type=png";
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" . $this->getAccessToken() . "&type=png";
|
|
|
$ch1 = curl_init();
|
|
|
$timeout = 5;
|
|
|
$real_path = "{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}";
|
...
|
...
|
|