PHP自定义实现微信分享朋友圈源码演示下载
发布于 2年前 作者 erdangjiade 722 次浏览 来自 分享
粉丝福利 : 关注VUE中文社区公众号,回复视频领取粉丝福利

演示下载参考下这里: http://www.erdangjiade.com/php/2746.html 效果图片如下: 完整代码如下: 1、先加载微信自带的接口代码

<?php class JSSDK { private $appId; private $appSecret;

public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}

public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();

// 注意 URL 一定要动态获取,不能 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();  

// 这里参数的顺序要按照 key 值 ASCII 码升序排序  
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$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;
}

回到顶部