服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - PHP教程 - php微信开发之图片回复功能

php微信开发之图片回复功能

2019-09-25 11:02qiphon3650 PHP教程

这篇文章主要为大家详细介绍了php微信开发之图片回复功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了php微信图片回复功能的具体代码,供大家参考,具体内容如下

<?php
/**
 * wechat php test
 */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
  public function valid()
  {
    $echoStr = $_GET["echostr"];

    //valid signature , option
    if($this->checkSignature()){
      echo $echoStr;
      exit;
    }
  }

  public function responseMsg()
  {
    //get post data, May be due to the different environments
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

    //extract post data
    if (!empty($postStr)){

        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $type = $postObj->MsgType;
        $customrevent = $postObj->Event;
        $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>";       
        if($type=="event" and $customrevent=="subscribe"){
          $contentStr = "33333333333";
          $msgType = "text";
          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
          echo $resultStr;
          }  
        if($type=="image" ){
          $contentStr = "你的图片很棒!";
          $msgType = "text";
          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
          echo $resultStr;
          }  

        if(!empty( $keyword ))
        {                
          $msgType = "text";
          if($keyword=="1"){
          $contentStr = "333";}
          if($keyword=="2"){
          $contentStr = "444
          if($keyword=="3"){
          $contentStr = "11";}          
          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
          echo $resultStr;
        }else{
          echo "Input something...";
        }

    }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;
    }
  }
}

?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • PHP教程PHP关于foreach复制知识点总结

    PHP关于foreach复制知识点总结

    在本篇文章里小编给大家分享了关于PHP关于foreach复制知识点总结,有兴趣的朋友们学习下。...

    laozhang3792019-06-26
  • PHP教程PHP PDOStatement::nextRowset讲解

    PHP PDOStatement::nextRowset讲解

    今天小编就为大家分享一篇关于PHP PDOStatement::nextRowset讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    php参考手册4102019-06-23
  • PHP教程php中的explode()函数实例介绍

    php中的explode()函数实例介绍

    在本篇文章里小编给大家分享了关于php中的explode()函数实例知识点内容,有兴趣的朋友们学习下。...

    laozhang4792019-06-30
  • PHP教程PHP hex2bin()函数用法讲解

    PHP hex2bin()函数用法讲解

    今天小编就为大家分享一篇关于PHP hex2bin()函数用法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    php参考手册3472019-06-10
  • PHP教程php微信公众号开发之校园图书馆

    php微信公众号开发之校园图书馆

    这篇文章主要为大家详细介绍了php微信公众号开发之校园图书馆,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    dq_0951302019-08-31
  • PHP教程PHP微信发送推送消息乱码的解决方法

    PHP微信发送推送消息乱码的解决方法

    今天小编就为大家分享一篇关于PHP微信发送推送消息乱码的解决方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    炫酷飞扬1632019-06-07
  • PHP教程Laravel框架实现的批量删除功能示例

    Laravel框架实现的批量删除功能示例

    这篇文章主要介绍了Laravel框架实现的批量删除功能,结合实例形式分析了Laravel框架批量删除功能相关的前端界面布局及后台控制器实现技巧,需要的朋友可以参考下...

    snow_small4782019-07-03
  • PHP教程PHP fclose函数用法总结

    PHP fclose函数用法总结

    我们在本篇文章里给大家整理了关于PHP中fclose函数的详细用法和知识点,需要的朋友们学习下。...

    laozhang3782019-06-14