zoukankan      html  css  js  c++  java
  • php实现回复图文,图片,文字

    
    
    
    
     

    [代码]php代码:

    <?php
    /**
         微信公众平台
    
         开发者模式
    
         默认用户输入任何文字,均返回同一个图文信息,链接地址为手机站;
    
         可以根据变量$keyword,即用户输入的信息,进行判断,从而返回相应的信息;
    
    */

    define
    ("TOKEN", "你自己的TOKEN");//与管理平台的TOKEN设置一致 $wechatObj = new wechatCallbackapiTest(); //$wechatObj->valid(); $wechatObj->responseMsg(); class wechatCallbackapiTest { public function valid()//验证接口用,管理平台后台设置的时候请调用此方法进行验证 { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } public 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; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; //加载图文模版 $picTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>1</ArticleCount> <Articles> <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> </Articles> <FuncFlag>1</FuncFlag> </xml> "; if(trim($postObj->MsgType) == "event" and trim($postObj->Event) == "subscribe")//判断是否是新关注 { /*$msgType = "text"; $contentStr = "您好,欢迎您关注PHP二次开发网"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;*/ $msgType = "news"; $title = "PHP二次开发网"; //标题 $data = date('Y-m-d'); //时间 $desription = "“php二次开发,ecshop二次开发,dedecms二次开发,discuz二次开发,wordpress二次开发,SEO,php2.cc,让我们共同学习。“"; //简介 $image = "http://www.php2.cc/weixin/img/proxy.jpeg"; //图片地址 $turl = "http://www.php2.cc/forum.php?mobile=yes"; //链接地址 $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl); echo $resultStr; }elseif(!empty($keyword ))//用户输入的内容 { $msgType = "news"; $title = "PHP二次开发网"; //标题 $data = date('Y-m-d'); //时间 $desription = "“php二次开发,ecshop二次开发,dedecms二次开发,discuz二次开发,wordpress二次开发,SEO,php2.cc,让我们共同学习。“"; //简介 $image = "http://www.php2.cc/weixin/img/proxy.jpeg"; //图片地址 $turl = "http://www.php2.cc/forum.php?mobile=yes"; //链接地址 $resultStr = sprintf($picTpl, $fromUsername, $toUsername, $time, $msgType, $title,$desription,$image,$turl); echo $resultStr; }else{ echo "说点什么吧!"; } }else { echo "请输入任意文字!"; exit; } } //封装的验证 private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
  • 相关阅读:
    前台组件
    IntelliJ IDEA 简体中文专题教程
    干货分享 | 创业公司绝对不会告诉你他们在用的工具们
    线程的基本概念 / 计算机程序的思维逻辑
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    【转载】 准人工智能分享Deep Mind报告 ——AI“元强化学习”
    (待续)【转载】 DeepMind发Nature子刊:通过元强化学习重新理解多巴胺
    【转载】 机器学习算法岗的常见面试问题及准备
    【转载】 180623 Conda install 本地压缩包文件tar.bz2
    2017年 某次 实验室会议 —— 记
  • 原文地址:https://www.cnblogs.com/xujing6/p/6099144.html
Copyright © 2011-2022 走看看