微信公众号生成新浪短网址 快速生成

有没有想过,向一个公众号发送长链接,然后公众号给你回复一个短网址?

其实很简单:

FromUserName;
        $toUsername = $postObj->ToUserName;
        $MsgT = $postObj->MsgType;
        $time = time();
        //如果用户发的text类型
        if($MsgT=="text"){
            $key = trim($postObj->Content);
            $textTpl = "
                        
                        
                        %s
                        
                        
                        "; 
            $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 = "
                        
                        
                        %s
                        
                        
                        "; 
            $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 = "
                        
                        
                        %s
                        
                        
                        "; 
            $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();
    }
?>

配置方法:

1、登录公众号
2、打开基本配置

3、填写上面代码的url

Token在代码里面自己设置,要对应的,EncodingAESKey随机生成。

4、然后启用这个配置,然后去公众号发送一个url看看!

体验:

作者:TANKING
网站:http://www.likeyunba.com/

你可能感兴趣的:(微信开发,公众号,短网址,微信公众号开发,php)