欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php微信公众号开发之图片回复

程序员文章站 2022-11-14 21:26:31
本文实例为大家分享了php微信公众号开发之图片回复的具体代码,供大家参考,具体内容如下 图片回复 随机函数: rand(1,10) 核心代码:...

本文实例为大家分享了php微信公众号开发之图片回复的具体代码,供大家参考,具体内容如下

图片回复

随机函数:

rand(1,10)

php微信公众号开发之图片回复

核心代码:

$tyep= $postobj->msgtype;
$texttpl = "
 <xml>
  <tousername><![cdata[%s]]></tousername>
  <fromusername><![cdata[%s]]></fromusername>
  <createtime>%s</createtime>
  <msgtype><![cdata[text]]></msgtype>
  <content><![cdata[%s]]></content>
  <funcflag>0</funcflag>
 </xml>";   

 if ($tyep=="image")
 {
  $a=rand(1,3);
   switch ($a)
   {
    case "1";
     $b="健康好人缘,婚姻幸福美满";
     break;
   case "2";
     $b="贵人相助化险为夷的好运纹";
     break;
    default;
     $b="才华横溢好研究,知识丰富事业有成";        
   }

   $resultstr = sprintf($texttpl, $fromusername, $tousername, $time,$b);
   echo $resultstr;
 }

index.php代码如下:

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

//define your token
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
$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;
    $keyword = trim($postobj->content);
    $time = time();
    $tyep= $postobj->msgtype;
    $texttpl = "
       <xml>
       <tousername><![cdata[%s]]></tousername>
       <fromusername><![cdata[%s]]></fromusername>
       <createtime>%s</createtime>
       <msgtype><![cdata[text]]></msgtype>
       <content><![cdata[%s]]></content>
       <funcflag>0</funcflag>
       </xml>";   


    if ($tyep=="image")
    {$a=rand(1,3);
     switch ($a)
     {case "1";
     $b="健康好人缘,婚姻幸福美满";
     break;
     case "2";
     $b="贵人相助化险为夷的好运纹";
     break;
     default;
     $b="才华横溢好研究,知识丰富事业有成";        
     }

     $resultstr = sprintf($texttpl, $fromusername, $tousername, $time,$b);
     echo $resultstr;
    }

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

?>

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