<?php
$appId = 'appid';
$appsecret = '密钥';
$timestamp = time();
$jsapi_ticket = make_ticket($appId,$appsecret);
$nonceStr = make_nonceStr();
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$signature = make_signature($nonceStr,$timestamp,$jsapi_ticket,$url);
function make_nonceStr()
{
$codeSet = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($i = 0; $i<16; $i++) {
$codes[$i] = $codeSet[mt_rand(0, strlen($codeSet)-1)];
}
$nonceStr = implode($codes);
return $nonceStr;
}
function make_signature($nonceStr,$timestamp,$jsapi_ticket,$url)
{
$tmpArr = array(
'noncestr' => $nonceStr,
'timestamp' => $timestamp,
'jsapi_ticket' => $jsapi_ticket,
'url' => $url
);
ksort($tmpArr, SORT_STRING);
$string1 = http_build_query( $tmpArr );
$string1 = urldecode( $string1 );
$signature = sha1( $string1 );
return $signature;
}
function make_ticket($appId,$appsecret)
{
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appsecret;
$json = file_get_contents($TOKEN_URL);
$result = json_decode($json,true);
$access_token = $result['access_token'];
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}else{
$access_token = $data->access_token;
}
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
$ticket_URL="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
$json = file_get_contents($ticket_URL);
$result = json_decode($json,true);
$ticket = $result['ticket'];
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}else{
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js微信自定义分享标题、链接和图标</title>
<meta name="keywords" content="js微信分享,php微信分享" />
<meta name="description" content="PHP自定义微信分享内容,包括标题、图标、链接等,分享成功和取消有js回调函数。" />
</head>
<body>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
wx.config({
debug: false,
appId: '<?=$appId?>',
timestamp: <?=$timestamp?>,
nonceStr: '<?=$nonceStr?>',
signature: '<?=$signature?>',
jsApiList: [
'scanQRCode','onMenuShareAppMessage','onMenuShareTimeline'
]
});
wx.ready(function (){
var shareData = {
title: '百度"', //朋友圈文案(朋友圈没有标题)
desc: '百度',
link: 'http://www.baidu.com',
imgUrl: ''
};
var shareData1 = {
title: '百度', //发送给朋友的标题
desc: '百度', //文案
link: 'http://www.baidu.com',
imgUrl: ''
};
wx.onMenuShareAppMessage(shareData1);
wx.onMenuShareTimeline(shareData);
});
</script>
<p style="text-align: center;color:red;font-size:20px;margin-top: 120px">请用微信浏览器打开,并打开右上方按钮。分享到朋友圈试试。</p>
</html>
$appId = 'appid';
$appsecret = '密钥';
$timestamp = time();
$jsapi_ticket = make_ticket($appId,$appsecret);
$nonceStr = make_nonceStr();
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$signature = make_signature($nonceStr,$timestamp,$jsapi_ticket,$url);
function make_nonceStr()
{
$codeSet = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($i = 0; $i<16; $i++) {
$codes[$i] = $codeSet[mt_rand(0, strlen($codeSet)-1)];
}
$nonceStr = implode($codes);
return $nonceStr;
}
function make_signature($nonceStr,$timestamp,$jsapi_ticket,$url)
{
$tmpArr = array(
'noncestr' => $nonceStr,
'timestamp' => $timestamp,
'jsapi_ticket' => $jsapi_ticket,
'url' => $url
);
ksort($tmpArr, SORT_STRING);
$string1 = http_build_query( $tmpArr );
$string1 = urldecode( $string1 );
$signature = sha1( $string1 );
return $signature;
}
function make_ticket($appId,$appsecret)
{
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appsecret;
$json = file_get_contents($TOKEN_URL);
$result = json_decode($json,true);
$access_token = $result['access_token'];
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}else{
$access_token = $data->access_token;
}
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
$ticket_URL="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
$json = file_get_contents($ticket_URL);
$result = json_decode($json,true);
$ticket = $result['ticket'];
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}else{
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js微信自定义分享标题、链接和图标</title>
<meta name="keywords" content="js微信分享,php微信分享" />
<meta name="description" content="PHP自定义微信分享内容,包括标题、图标、链接等,分享成功和取消有js回调函数。" />
</head>
<body>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
wx.config({
debug: false,
appId: '<?=$appId?>',
timestamp: <?=$timestamp?>,
nonceStr: '<?=$nonceStr?>',
signature: '<?=$signature?>',
jsApiList: [
'scanQRCode','onMenuShareAppMessage','onMenuShareTimeline'
]
});
wx.ready(function (){
var shareData = {
title: '百度"', //朋友圈文案(朋友圈没有标题)
desc: '百度',
link: 'http://www.baidu.com',
imgUrl: ''
};
var shareData1 = {
title: '百度', //发送给朋友的标题
desc: '百度', //文案
link: 'http://www.baidu.com',
imgUrl: ''
};
wx.onMenuShareAppMessage(shareData1);
wx.onMenuShareTimeline(shareData);
});
</script>
<p style="text-align: center;color:red;font-size:20px;margin-top: 120px">请用微信浏览器打开,并打开右上方按钮。分享到朋友圈试试。</p>
</html>