微信分享接口示例(設置標題、縮略圖、連接、描述)

前幾天因為項目所需要實現(xiàn)微信分享接口,在網(wǎng)上搜了一大堆,實現(xiàn)辦法大致分為兩種,第一:在body之后加一個img標簽并且設置display:none,這種方法感覺不科學所以我沒有測試過。第二:使用微信的分享接口,但在網(wǎng)上也沒用找到完整的示例,還是自己折騰吧,請看下面。

實現(xiàn)工具:GetwxLink

注意:1:jsapi_ticket和access_token需要存在本地,access_token微信一天限制獲取2000次

????????2:獲取access_token之前先確認ip地址在白名單內(nèi)!!!

第一步:

先登錄微信公眾平臺進入“公眾號設置”的“功能設置”里填寫“JS接口安全域名”。

注:認證帳號才有分享權(quán)限

第二步

創(chuàng)建一個demo.php文件和wxshare.js

demo.php

// 步驟1.設置appid和appsecret

? ? private $WX_APPID="wx9b1ec4fef5bf666";

? ? private $WX_SECRET="8e3e63698ddac81947f52b0a7be45b6666";

? ? // 步驟2.生成簽名的隨機串

? ? function nonceStr($length){

? ? ? ? $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62個字符

? ? ? ? $strlen = 62;

? ? ? ? while($length > $strlen){

? ? ? ? ? ? $str .= $str;

? ? ? ? ? ? $strlen += 62;

? ? ? ? }

? ? ? ? $str = str_shuffle($str);

? ? ? ? return substr($str,0,$length);

? ? }

? ? // 步驟3.獲取access_token

? ? function getAccessToken()

? ? {

? ? ? ? // access_token 應該全局存儲與更新,以下代碼以寫入到文件中做示例

? ? ? ? $data = json_decode(self::get_php_file(WEIXIN."/access_token.php"));

//? ? ? ? var_dump($data);exit;

? ? ? ? if ($data->expire_time < time()) {

? ? ? ? ? ? // 如果是企業(yè)號用以下URL獲取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(self::httpGet($url));

? ? ? ? ? ? $access_token = $res->access_token;

? ? ? ? ? ? if ($access_token) {

? ? ? ? ? ? ? ? $data->expire_time = time() + 7000;

? ? ? ? ? ? ? ? $data->access_token = $access_token;

? ? ? ? ? ? ? ? self::set_php_file(WEIXIN."/access_token.php", json_encode($data));

? ? ? ? ? ? }

? ? ? ? } else {

? ? ? ? ? ? $access_token = $data->access_token;

? ? ? ? }

? ? ? ? return $access_token;

? ? }

? ? // 步驟4.獲取ticket

? ? function ticket()

? ? {

? ? ? ? // jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例

? ? $data = json_decode($this->get_php_file(WEIXIN."/jsapi_ticket.php"));

? ? $data1 = json_decode($this->get_php_file(WEIXIN."/access_token.php"));

? ? if ($data->expire_time < time()) {

? ? ? $accessToken = $this->getAccessToken();

? ? ? // 如果是企業(yè)號用以下 URL 獲取 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));

? ? ? ? //var_dump($res);exit;

? ? ? ? if($res->errcode==40001){//token過期重新生成

? ? ? ? ? ? $data1->expire_time = 0;

? ? ? ? ? ? $data1->access_token = '';

? ? ? ? ? ? $this->set_php_file(WEIXIN."access_token.php", json_encode($data1));

? ? ? ? ? ? $data->expire_time = 0;

? ? ? ? ? ? $data->jsapi_ticket = '';

? ? ? ? ? ? $this->set_php_file(WEIXIN."jsapi_ticket.php", json_encode($data));

? ? ? ? ? ? $this->getSignPackage();//重新執(zhí)行下請求token的方法

? ? ? ? }

? ? ? ? $ticket = $res->ticket;

? ? ? if ($ticket) {

? ? ? ? $data->expire_time = time() + 7000;

? ? ? ? $data->jsapi_ticket = $ticket;

? ? ? ? $this->set_php_file(WEIXIN."/jsapi_ticket.php", json_encode($data));

? ? ? }

? ? } else {

? ? ? $ticket = $data->jsapi_ticket;

? ? }

? ? return $ticket;

? ? }

? ? // 步驟5.生成wx.config需要的參數(shù)

? ? function share()

? ? {

? ? ? ? $surl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

//? ? ? ? var_dump(self::getAccessToken());exit;

? ? ? ? return self::getWxConfig(self::ticket(), $surl, time(), self::nonceStr(16));

? ? }

? ? function getWxConfig($jsapiTicket,$url,$timestamp,$nonceStr) {

? ? ? ? $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

? ? ? ? $signature = sha1 ( $string );

? ? ? ? $WxConfig["appId"] = $this->WX_APPID ;

? ? ? ? $WxConfig["nonceStr"] = $nonceStr;

? ? ? ? $WxConfig["timestamp"] = $timestamp;

? ? ? ? $WxConfig["url"] = $url;

? ? ? ? $WxConfig["signature"] = $signature;

? ? ? ? $WxConfig["rawString"] = $string;

? ? ? ? return $WxConfig;

? ? }

function http_get($url){

? ? ? ? $oCurl = curl_init();

? ? ? ? if(stripos($url,"https://")!==FALSE){

? ? ? ? ? ? curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);

? ? ? ? ? ? curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);

? ? ? ? ? ? curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1

? ? ? ? }

? ? ? ? curl_setopt($oCurl, CURLOPT_URL, $url);

? ? ? ? curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );

? ? ? ? $sContent = curl_exec($oCurl);

? ? ? ? $aStatus = curl_getinfo($oCurl);

? ? ? ? curl_close($oCurl);

? ? ? ? if(intval($aStatus["http_code"])==200){

? ? ? ? ? ? return $sContent;

? ? ? ? }else{

? ? ? ? ? ? return false;

? ? ? ? }

? ? }

/*get方法*/

? private function httpGet($url) {

? ? $curl = curl_init();

? ? curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

? ? curl_setopt($curl, CURLOPT_TIMEOUT, 500);

? ? // 為保證第三方服務器與微信服務器之間數(shù)據(jù)傳輸?shù)陌踩?,所有微信接口采用https方式調(diào)用,必須使用下面2行代碼打開ssl安全校驗。

? ? // 如果在部署過程中代碼在此處驗證失敗,請到 http://curl.haxx.se/ca/cacert.pem 下載新的證書判別文件。

? ? curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

? ? curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);

? ? curl_setopt($curl, CURLOPT_URL, $url);

? ? $res = curl_exec($curl);

? ? curl_close($curl);

? ? return $res;

? }

/*從文件中獲取token*/

? ? public function get_php_file($filename) {

? ? return trim(substr(file_get_contents($filename), 15));

? }

/*存儲到文件中*/

? public function set_php_file($filename, $content) {

? ? $fp = fopen($filename, "w");

? ? fwrite($fp, "<?php exit();?>" . $content);//防止執(zhí)行php腳本,確保linux文件可寫

? ? fclose($fp);

? }網(wǎng)頁部分:

<!DOCTYPE html>

<html?lang="en">

<head>

<meta?charset="UTF-8">

<title>Share Demo</title>

</head>

<body>

</body>

// 步驟6.調(diào)用JS接口

<script?src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<script>

wx.config({

debug: false,

? ? appId: '<?php echo $detail["share"]["appId"]; ?>',

? ? timestamp: '<?php echo $detail["share"]["timestamp"]; ?>',

? ? nonceStr: '<?php echo $detail["share"]["nonceStr"]; ?>',

? ? signature: '<?php echo $detail["share"]["signature"]; ?>',

? ? jsApiList: [

? ? ? ? 'checkJsApi',

? ? ? ? 'onMenuShareTimeline',

? ? ? ? 'onMenuShareAppMessage',

? ? ? ? 'onMenuShareQQ',

? ? ? ? 'onMenuShareWeibo',

? ? ? ? 'onMenuShareQZone',

? ? ]

});

var wstitle = "<?php echo $detail['act_ext2']; ?>號選手:<?php echo $detail['act_name']; ?>";

var wsdesc = "<?php echo $detail['act_beta']; ?>";

var wslink = "<?php echo $detail['share']['url']; ?>";

var wsimg = "http://<?php echo $detail['share_pic']; ?>";

</script>

<script?src="wxshare.js"></script>

</html>

wxshare.js

wx.ready(function () {

// 分享到朋友圈

wx.onMenuShareTimeline({

title: wstitle,

link: wslink,

imgUrl: wsimg,

success: function () {

alert('分享成功');

},

cancel: function () {

}

});

// 分享給朋友

wx.onMenuShareAppMessage({

title: wstitle,

desc: wsdesc,

link: wslink,

imgUrl: wsimg,

success: function () {

alert('分享成功');

},

cancel: function () {

}

});

// 分享到QQ

wx.onMenuShareQQ({

title: wstitle,

desc: wsdesc,

link: wslink,

imgUrl: wsimg,

success: function () {

alert('分享成功');

},

cancel: function () {

}

});

// 微信到騰訊微博

wx.onMenuShareWeibo({

title: wstitle,

desc: wsdesc,

link: wslink,

imgUrl: wsimg,

success: function () {

alert('分享成功');

},

cancel: function () {

}

});

// 分享到QQ空間

wx.onMenuShareQZone({

title: wstitle,

desc: wsdesc,

link: wslink,

imgUrl: wsimg,

success: function () {

alert('分享成功');

},

cancel: function () {

}

});

});

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容