微信公眾號生成新浪短網(wǎng)址的實現(xiàn)(快速生成)
有沒有想過,向一個公眾號發(fā)送長鏈接,然后公眾號給你回復(fù)一個短網(wǎng)址?
其實很簡單:

<?php
define("TOKEN", "xiao");
//用于回復(fù)用戶消息
function responseMsg(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$MsgT = $postObj->MsgType;
$time = time();
//如果用戶發(fā)的text類型
if($MsgT=="text"){
$key = trim($postObj->Content);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$msgType = "text";
//生成短網(wǎng)址
$dwzapi = "http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=".$key;
$dwzpost = file_get_contents($dwzapi);
$dwzjsondecode = json_decode($dwzpost,true);
//發(fā)送
$contentStr = $dwzjsondecode[0]['url_short'];
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}else{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$msgType = "text";
//發(fā)送
$contentStr = "請發(fā)送鏈接";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
//如果用戶發(fā)的event(事件)類型
if($MsgT=="event"){
$Event = $postObj->Event;
if ($Event==subscribe) {
$contentStr = "歡迎關(guān)注";
}else{
$contentStr = "希望您下次關(guān)注,但您收不到此條消息了";
}
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$Title = $postObj->Title;
$Description = $postObj->Description;
$Url = $postObj->Url;
$msgType = 'text';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
exit;
}
}else{
echo "不對";
exit;
}
}
$echoStr = $_GET["echostr"];
//如果有$echoStr說明是對接
if (!empty($echoStr)) {
//對接規(guī)則
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
echo $echoStr;
}else{
echo "";
exit;
}
}else{
responseMsg();
}
?>
配置方法:
1、登錄公眾號
2、打開基本配置

3、填寫上面代碼的url

Token在代碼里面自己設(shè)置,要對應(yīng)的,EncodingAESKey隨機(jī)生成。
4、然后啟用這個配置,然后去公眾號發(fā)送一個url看看!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js 調(diào)用本地exe的例子(支持IE內(nèi)核的瀏覽器)
js 調(diào)用本地exe程序.我實驗了一下 : 使用IE內(nèi)核的瀏覽器 都支持 火狐好像不行,感興趣的碰可以研究下2012-12-12
javascript+ajax實現(xiàn)產(chǎn)品頁面加載信息
本文給大家分享的是使用javascript結(jié)合ajax實現(xiàn)產(chǎn)品頁面無刷新加載信息的代碼,非常的簡單實用,有需要的小伙伴可以參考下。2015-07-07
使用JavaScript實現(xiàn)表格編輯器(實例講解)
下面小編就為大家?guī)硪黄褂肑avaScript實現(xiàn)表格編輯器(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
el-popover放在el-table中點(diǎn)擊無反應(yīng)問題解決方案
我們想在table中給btn加彈框但是?el-popover點(diǎn)擊按鈕沒有任何反應(yīng),解決思路是通過給每個el-popover都加上單獨(dú)的id,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-04-04
有關(guān)文件上傳 非ajax提交 得到后臺數(shù)據(jù)問題
本文給大家介紹關(guān)于文件上傳非ajax提交得到后臺數(shù)據(jù)的問題我們該怎么處理呢?下文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2016-10-10
JS實現(xiàn)把鼠標(biāo)放到鏈接上出現(xiàn)滾動文字的方法
這篇文章主要介紹了JS實現(xiàn)把鼠標(biāo)放到鏈接上出現(xiàn)滾動文字的方法,涉及JavaScript響應(yīng)鼠標(biāo)事件動態(tài)操作頁面元素的相關(guān)技巧,需要的朋友可以參考下2016-04-04

