微信公衆號生成新浪短網址 快速生成

有沒有想過,向一個公衆號發送長連接,而後公衆號給你回覆一個短網址?php

其實很簡單:json

clipboard.png

<?php
define("TOKEN", "xiao");
//用於回覆用戶消息
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();
        //若是用戶發的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";

            //生成短網址
            $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);

            //發送
            $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";
        
            //發送
            $contentStr =  "請發送連接";
            

            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
            exit;
        }

        //若是用戶發的event(事件)類型
        if($MsgT=="event"){
            $Event = $postObj->Event;
            if ($Event==subscribe) {
               $contentStr = "歡迎關注";
            }else{
                $contentStr = "但願您下次關注,但您收不到此條消息了";
            }
            $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)) {
        //對接規則
        $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();
    }
?>

配置方法:

一、登陸公衆號
二、打開基本配置api

clipboard.png

三、填寫上面代碼的urlpost

clipboard.png

Token在代碼裏面本身設置,要對應的,EncodingAESKey隨機生成。網站

四、而後啓用這個配置,而後去公衆號發送一個url看看!url

體驗:spa

圖片描述

做者:TANKING
網站:http://www.likeyunba.com/3d

相關文章
相關標籤/搜索