微信分享php方法
<?php
/**
* 微信授权
* 微信分享
* @author rex
*/
class WechatShare extends CI_Controller{
//高级功能-》开发者模式-》获取
private $app_id = 'appid';
private $app_secret = 'appsecret';
public function __construct(){
parent::__construct();
$this->load->model('Access_token_model');
$this->load->model('Ticket_model');
}
/**
* 获取微信授权链接
* @author rex
* @param string $redirect_uri 跳转地址
* @param mixed $state 参数
*/
public function get_authorize_url($redirect_uri = '', $state = '')
{
$redirect_uri = urlencode($redirect_uri);
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
}
/**
* 获取授权token
* @author rex
* @param string $code 通过get_authorize_url获取到的code
*/
function wx_get_token() {
$tinfo = $this->Access_token_model->getlast();
if($tinfo && time()-$tinfo['add_time'] < 5000){
$token = $tinfo['token'];
return token;
exit;
}else{
$res = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret');
$res = json_decode($res, true);
$token = $res['access_token'];
if($token){
$where = array();
$where['token'] = $token;
$where['add_time'] = time();
$this->Access_token_model->add($where);
}
// 注意:这里需要将获取到的token缓存起来(或写到数据库中)
// 不能频繁的访问https://api.weixin.qq.com/cgi-bin/token,每日有次数限制
// 通过此接口返回的token的有效期目前为2小时。令牌失效后,JS-SDK也就不能用了。
// 因此,这里将token值缓存1小时,比2小时小。缓存失效后,再从接口获取新的token,这样
// 就可以避免token失效。
// S()是ThinkPhp的缓存函数,如果使用的是不ThinkPhp框架,可以使用你的缓存函数,或使用数据库来保存。
return $token;
}
}
function wx_get_jsapi_ticket(){
$ticket = "";
do{
$token = $this->wx_get_token();
$tinfo = $this->Ticket_model->getlast();
if($tinfo && time()-$tinfo['add_time'] < 5000){
$ticket = $tinfo['ticket'];
}else{
$url2 = sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi",$token);
$res = file_get_contents($url2);
$res = json_decode($res, true);
$ticket = $res['ticket'];
if($ticket){
$where = array();
$where['ticket'] = $ticket;
$where['add_time'] = time();
$this->Ticket_model->add($where);
}
}
// 注意:这里需要将获取到的ticket缓存起来(或写到数据库中)
// ticket和token一样,不能频繁的访问接口来获取,在每次获取后,我们把它保存起来。
return $ticket;
}while(0);
return $ticket;
}
/**
* 分享页面
* @param string
* @param string
*/
public function see_index(){
$timestamp = time();
$wxnonceStr = "SIFJASF";
//$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $_POST['url'];
$wxticket = $this->wx_get_jsapi_ticket();
$wxOri = sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s",$wxticket, $wxnonceStr, $timestamp,$url);
$wxSha1 = sha1($wxOri);
$data = array();
$data['time'] = $timestamp;
$data['str'] = $wxnonceStr;
$data['sign'] = $wxSha1;
$data['test'] = $wxticket;
exit(json_encode($data));
}
public function main(){
templates('wxmain','index');
}
public function http($url, $method, $postfields = null, $headers = array(), $debug = false)
{
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
$this->postdata = $postfields;
}
break;
}
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ci);
$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
if ($debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo '=====info=====' . "\r\n";
print_r(curl_getinfo($ci));
echo '=====$response=====' . "\r\n";
print_r($response);
}
curl_close($ci);
return array($http_code, $response);
}
}
html页面的js
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript" src="http://www.test.cn/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var url = location.href.split('#')[0];
$.ajax({
url:'请求地址see_index',
type:"POST",
data:{'url':url},
success: function(data) {
var json = eval('(' + data + ')');
// 微信配置
var time = json.time;
wx.config({
debug: false,
appId: "appid",
timestamp: json.time,
nonceStr: json.str,
signature: json.sign,
jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 功能列表,我们要使用JS-SDK的什么功能
});
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在 页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready 函数中。
wx.ready(function(){
// 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
wx.onMenuShareTimeline({
title: 'hello', // 分享标题
desc: '这是一个分享描述', // 分享描述
link:"https://www.baidu.com/",
imgUrl: "图标url", // 分享图标
success: function () {
alert('分享成功');
// 用户确认分享后执行的回调函数
}
});
// 获取“分享给朋友”按钮点击状态及自定义分享内容接口
wx.onMenuShareAppMessage({
title: '标题', // 分享标题
desc: "描述", // 分享描述
link:"https://www.baidu.com/",
imgUrl: "图标地址", // 分享图标
type: 'link', // 分享类型,music、video或link,不填默认为link
});
});
}
});
})
</script>