当前位置:主页 > 网页前端 > JavaScript代码 >

微信公众号生成新浪短网址的实现(快速生成)

时间:2022-06-26 10:34:51 | 栏目:JavaScript代码 | 点击:

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

其实很简单:

<?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();
  }
?>

配置方法:

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

3、填写上面代码的url

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

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

您可能感兴趣的文章:

相关文章