Implementation code of php sharing circle of friends
- 2021-11-29 06:24:01
- OfStack
In this paper, we share the specific code of php to share friends circle for your reference. The specific contents are as follows
<?php
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// Attention URL 1 You must get it dynamically, you cannot hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// The order of parameters here should be as follows key Value ASCII Code ascending sort
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr × tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
private function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
private function getJsApiTicket() {
// jsapi_ticket It should be stored and updated globally. The following code is written to a file as an example
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
// If it is an enterprise, use the following URL Get ticket
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->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;
}
private function getAccessToken() {
// access_token It should be stored and updated globally. The following code is written to a file as an example
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
// If it is an enterprise, use the following URL Get access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$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;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
$jssdk = new JSSDK("wx6b3844d6802f74aa", "c8710c8f4e0afce7611f5cd0013c4573");
$signPackage = $jssdk->GetSignPackage();
?>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
/*
* Note:
* 1. All JS The interface can only be called in the domain name bound by WeChat official account. WeChat official account developers need to log in to WeChat public platform and enter the "Function Settings" of "WeChat official account Settings" to fill in " JS Interface security domain name ".
* 2. If it is found in Android Unable to share custom content, please download the latest package overlay installation in official website. Android Custom sharing interface needs to be upgraded to 6.0.2.58 Version and above.
* 3. Frequently Asked Questions and Complete JS-SDK Document address: http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
*
* See the document "Appendix" for details of problems encountered in development 5- Common errors and solutions ", if it still fails to be solved, feedback can be given through the following channels:
* Email address: weixin-open@qq.com
* Email subject: "WeChat JS-SDK Feedback "Specific questions
* Email content description: Describe the problem in concise language, and explain the scene of encountering the problem clearly. You can attach screenshots, and the WeChat team will handle your feedback as soon as possible.
*/
wx.config({
debug: false,
appId: '<?php echo $signPackage["appId"];?>',
timestamp: <?php echo $signPackage["timestamp"];?>,
nonceStr: '<?php echo $signPackage["nonceStr"];?>',
signature: '<?php echo $signPackage["signature"];?>',
jsApiList: ['onMenuShareTimeline',
'onMenuShareAppMessage'
// To invoke all of the API Add to this list
]
});
wx.ready(function () {
// Share friends
wx.onMenuShareAppMessage({
title: ' Your Share Title ', // Share title
desc: ' Your shared description ', // Share description
link: " Your link ?pid=<?php echo $userone['id']?>", // Share link
imgUrl: ' Picture address ', // Share icon
type: '', // Sharing type ,music , video Or link If not, default is link
dataUrl: '', // If type Yes music Or video To provide a data link, which is blank by default
success: function () {
//alert(' Successfully share with your friends ');
},
cancel: function () {
//alert(' Cancel sharing to your friends ');
// Callback function executed after user cancels sharing
}
});
// Friends circle
wx.onMenuShareTimeline({
title: ' Your Share Title ', // Share title
desc: ' Your shared description ', // Share description
link: " Your link ?pid=<?php echo $userone['id']?>", // Share link
imgUrl: ' Picture address ', // Share icon
success: function () {
// Callback function executed after the user confirms sharing
},
cancel: function () {
// Callback function executed after user cancels sharing
}
});
});
</script>