栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

其实很简单:

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看看!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/74249.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号