日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微信分享接口示例(设置标题、缩略图、连接、描述),附demo下载

發(fā)布時(shí)間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信分享接口示例(设置标题、缩略图、连接、描述),附demo下载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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


第一步:

先登錄微信公眾平臺進(jìn)入“公眾號設(shè)置”的“功能設(shè)置”里填寫“JS接口安全域名”。
注:認(rèn)證帳號才有分享權(quán)限

第二步

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

demo.php

<?php//?步驟1.設(shè)置appid和appsecret$appid?=?'wxd75a2b20d3a54752';$appsecret?=?'9b32270f32874ea7a7427f88ff770777';//?步驟2.生成簽名的隨機(jī)串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$result?=?http_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret);$json?=?json_decode($result,true);$access_token?=?$json['access_token'];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;}}//?步驟4.獲取ticket$url?=?"https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";$res?=?json_decode?(?http_get?(?$url?)?);$ticket?=?$res->ticket;//?步驟5.生成wx.config需要的參數(shù)$surl?=?'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];$ws?=?getWxConfig(?$ticket,$surl,time(),nonceStr(16)?);function?getWxConfig($jsapiTicket,$url,$timestamp,$nonceStr)?{$string?=?"jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";$signature?=?sha1?(?$string?);$WxConfig["appId"]?=?$appid;$WxConfig["nonceStr"]?=?$nonceStr;$WxConfig["timestamp"]?=?$timestamp;$WxConfig["url"]?=?$url;$WxConfig["signature"]?=?$signature;$WxConfig["rawString"]?=?$string;return?$WxConfig;}?><!DOCTYPE?html> <html> <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?$ws["appId"];??>',timestamp:?'<?php?echo?$ws["timestamp"];??>',nonceStr:?'<?php?echo?$ws["nonceStr"];??>',signature:?'<?php?echo?$ws["signature"];??>',jsApiList:?['checkJsApi','onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone',]});var?wstitle?=?"我是標(biāo)題";var?wsdesc?=?"我是描述";var?wslink?=?"<?php?echo?$surl;??>";var?wsimg?=?"http://fmwei.com/usr/uploads/2016/07/794257096.png";</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?()?{}});//?分享到QQwx.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?()?{}});});


GetwxLink


轉(zhuǎn)載于:https://blog.51cto.com/14366269/2401963

總結(jié)

以上是生活随笔為你收集整理的微信分享接口示例(设置标题、缩略图、连接、描述),附demo下载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。