PHP
·
发表于 5年以前
·
阅读量:8296
±¾ÎÄʵÀý½²ÊoÁË»uÓÚthinkPHPʵÏÖµÄ΢ÐÅ×Ô¶¨ÒaÖÏi¹¦ÄÜ¡£ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º
ÔÚÐi¶a´oµÄÍøÕ¾ÎÒÃǶ¼»a¿´µ½µa»÷ÖÏi¾Í¿ÉÒÔ°ÑÊý¾ÝÖÏiµ½Î¢ÐÅ»o£Ñ£Ñ»oÆaËuµÄƽ̨ÁË,ÏÂÃaeÎÒÃÇÀ´¿´Ò»¶Îphp°ae΢ÐÅ×Ô¶¨ÒaÖÏi´uÂe,´uÂe²Î¿¼¹Ù½¿ª*¢µÄûÓÐÈκÎÎÊÌa.
ÖÏiÐeÒªÈÏ֤΢ÐŶ(C)ÔĺŻoÕßþÎñºÅ.
php ´uÂe(thinkphp):
$appid='xxx';
$appsecret='xxxx';
$timestamp = time();
$noncestr = $this->getRandStr(15);
// dump();
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='. $this->get_token($appid,$appsecret) .'&type;=jsapi';
$ret_json = $this->curl_get_contents($url);
$ret = json_decode($ret_json);
$ticket = $ret-> ticket;
//var_dump($ret);
$strvalue = 'jsapi_ticket='.$ticket.'&noncestr;='.$noncestr.'×tamp;='.$timestamp.'&url;=http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$signature = sha1($strvalue);
$this->assign('timestamp',$timestamp);
$this->assign('nonceStr',$noncestr);
$this->assign('signature',$signature);
function get_token($appid,$appsecret){
if(S('access_token')) return S('access_token');
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid;=$appid&secret;=$appsecret";
$ret_json = $this->curl_get_contents($url);
$ret = json_decode($ret_json);
if($ret -> access_token){
S('access_token',$ret -> access_token,7200);
return $ret -> access_token;
}
}
function is_weixin(){
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
return true;
}
return false;
}
function getRandStr($length){
$str = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randString = '';
$len = strlen($str)-1;
for($i = 0;$i < $length;$i ++){
$num = mt_rand(0, $len);
$randString .= $str[$num];
}
return $randString;
}
function curl_get_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 200);
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
curl_setopt($ch, CURLOPT_REFERER, _REFERER_);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
js´uÂe:ÐeÒªÒýÈe:http://res.wx.qq.com/open/js/jweixin-1.0.0.js
wx.config({
debug: false, // ¿ªÆoµ÷ÊÔģʽ,µ÷ÓõÄËuÓÐapiµÄ*µ»ØÖµ»aÔÚ¿Í»§¶Ëalert³oÀ´£¬ÈoÒª²e¿´´«ÈeµÄ²ÎÊý£¬¿ÉÒÔÔÚpc¶Ë´o¿ª£¬²ÎÊýÐÅÏ¢»aͨ¹ýlog´o³o£¬½oÔÚpc¶Ëʱ²Å»a´oÓ¡¡£
appId: 'wxae7c36a1349c5868', // ±ØÌi£¬¹«ÖںŵÄΨһ±eʶ
timestamp: '{$timestamp}', // ±ØÌi£¬Éu³ÉÇ(C)ÃuµÄʱ¼a´Á
nonceStr: '{$nonceStr}', // ±ØÌi£¬Éu³ÉÇ(C)ÃuµÄËae»u´®
signature: '{$signature}',// ±ØÌi£¬Ç(C)Ãu£¬¼u¸½Â¼1
jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // ±ØÌi£¬ÐeҪʹÓõÄJS½Ó¿ÚÁбi£¬ËuÓÐJS½Ó¿ÚÁбi¼u¸½Â¼2
});
wx.ready(function(){
wx.onMenuShareTimeline({
title: '{$contentInfo.title}', // *ÖÏi±eÌa
link: window.location.href, // *ÖÏiÁ´½Ó
imgUrl: 'http://'+window.location.host+'{$categoryInfo.image}', // *ÖÏiͼ±e
success: function () {
// Óû§È*ÈÏ*ÖÏiºoÖ´ÐеĻص÷º¯Êý
//alert(1111);
//fxfunc();
},
cancel: function () {
// Óû§È¡Ïu*ÖÏiºoÖ´ÐеĻص÷º¯Êý
//alert("ÄuÈ¡ÏuÁË*ÖÏi");
}
});
wx.onMenuShareAppMessage({
title: '{$contentInfo.title}', // *ÖÏi±eÌa
desc: removeHTMLTag('{$contentInfo.content}'), // *ÖÏiÃeÊo
link: window.location.href, // *ÖÏiÁ´½Ó
imgUrl: 'http://'+window.location.host+'{$categoryInfo.image}', // *ÖÏiͼ±e
type: '', // *ÖÏiÀaÐÍ,music¡¢video»olink£¬²»ÌiĬÈÏΪlink
dataUrl: '', // Èç¹utypeÊÇmusic»ovideo£¬ÔoÒªÌa¹(C)Êý¾ÝÁ´½Ó£¬Ä¬ÈÏΪ¿Õ
success: function () {
// Óû§È*ÈÏ*ÖÏiºoÖ´ÐеĻص÷º¯Êý
//fxfunc();
},
cancel: function () {
//alert("ÄuÈ¡ÏuÁË*ÖÏi");
// Óû§È¡Ïu*ÖÏiºoÖ´ÐеĻص÷º¯Êý
}
});
// configÐÅÏ¢ÑeÖ¤ºo»aÖ´ÐÐready*½*¨£¬ËuÓнӿڵ÷Óö¼±ØÐeÔÚconfig½Ó¿Ú»ñµÃ½a¹uÖ®ºo£¬configÊÇÒ»¸o¿Í»§¶ËµÄÒi²½²Ù×÷£¬ËuÒÔÈç¹uÐeÒªÔÚÒ³Ãae¼ÓÔØʱ¾Íµ÷ÓÃÏa¹Ø½Ó¿Ú£¬ÔoÐe°ÑÏa¹Ø½Ó¿Ú*ÅÔÚreadyº¯ÊýÖе÷ÓÃÀ´È*±£ÕýÈ*Ö´ÐС£¶ÔÓÚÓû§´¥*¢Ê±²Åµ÷ÓõĽӿڣ¬Ôo¿ÉÒÔÖ±½Óµ÷Ó㬲»ÐeÒª*ÅÔÚreadyº¯ÊýÖС£
});
function removeHTMLTag(str) {
str = str.replace(/<\/?[^>]*>/g,''); //È¥³ýHTML tag
str = str.replace(/[ | ]*\n/g,'\n'); //È¥³ýÐÐβ¿Õ°×
//str = str.replace(/\n[\s| | ]*\r/g,'\n'); //È¥³ý¶aÓa¿ÕÐÐ
str=str.replace(/ /ig,'');//È¥µo
return str;
}
¸u¶a¹ØÓÚPHPÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶PHP΢ÐÅ¿ª*¢¼¼ÇÉ»a×Ü¡¡¢¡¶PHP±aÂeÓeתÂe²Ù×÷¼¼ÇÉ»a×Ü¡¡¢¡¶PHPÍøÂç±a³Ì¼¼ÇÉ×ܽa¡¡¢¡¶[PHP»u±¾Ói¨ÈeÃŽ̳Ì](http://www.jb51.net/Special/348.htm)¡¡¢¡¶[php×Öu´®(string)Óè×ܽa](http://www.jb51.net/Special/47.htm)¡¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼ÒPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£