php微信公众号开发之欢迎老朋友

本文实例为大家分享了php微信公众号开发之欢迎老朋友的具体代码,供大家参考,具体内容如下

介绍数据库

  • insert
  • iconv 进行数据库转码函数
  • fromusername 

 php微信公众号开发之欢迎老朋友

关键代码如下:

$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>";  

     if(!empty( $keyword ))
     {
       //用 户 名 : $user
       //密  码 : $pwd
       //主库域名 : $host
       //从库域名 : SAE_MYSQL_HOST_S
       //端  口 : $port
       //数据库名 : $dbname

       $dbname = "app_dq095";
       $host = "w.rdc.sae.sina.com.cn";
       $port = "3306";
       $user = "4k514n103z";
       $pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";

       /*接着调用mysql_connect()连接服务器*/
       // 连主库
       $db = mysql_connect($host,$user,$pwd);

       if(!$db){
         die("Connect Server Failed: " . mysql_error($db));
       }

       /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/
       if (!mysql_select_db($dbname)) {
         die("Select Database Failed: " . mysql_error($db));
       }
   mysql_query("set names utf-8",$db); 

       /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/
       /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/
       /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/

       /**
       * 接下来就可以使用其它标准php mysql函数操作进行数据库操作
       */

       $sql="SELECT * FROM `welcome`WHERE `user`= '" . iconv("UTF-8","GBK",$fromUsername) . "'";
       $query=mysql_query($sql);
       $rs=mysql_fetch_array($query);
       $b= $rs['user'];
       $c=iconv("GBK","UTF-8",$b);
       $msgType = "text";
       if ($c==$fromUsername)
       {
         $contentStr = "欢迎老朋友!";
       }else{
        $sql="INSERT INTO `welcome`(`id`,`user`) VALUES (NULL,'{$fromUsername}')";
  mysql_query($sql);
$contentStr = "欢迎新朋友!";
       }

       $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);         
       echo $resultStr;
       mysql_close($db);
     }else{
         echo "Input something...";
 }

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();
        $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(!empty( $keyword ))
        {
          //用 户 名 : $user
          //密  码 : $pwd
          //主库域名 : $host
          //从库域名 : SAE_MYSQL_HOST_S
          //端  口 : $port
          //数据库名 : $dbname

          $dbname = "app_dq095";
          $host = "w.rdc.sae.sina.com.cn";
          $port = "3306";
          $user = "4k514n103z";
          $pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";

          /*接着调用mysql_connect()连接服务器*/
          // 连主库
          $db = mysql_connect($host,$user,$pwd);

          if(!$db){
            die("Connect Server Failed: " . mysql_error($db));
          }

          /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/
          if (!mysql_select_db($dbname)) {
            die("Select Database Failed: " . mysql_error($db));
          }
           mysql_query("set names utf-8",$db); 

          /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/
          /*!!!注意,无法再通过本次连接调用mysql_select_db来切换到其它数据库了!!!*/
          /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/

          /**
           * 接下来就可以使用其它标准php mysql函数操作进行数据库操作
           */

           $sql="SELECT * FROM `welcome`WHERE `user`= '" . iconv("UTF-8","GBK",$fromUsername) . "'";
           $query=mysql_query($sql);
           $rs=mysql_fetch_array($query);
           $b= $rs['user'];
           $c=iconv("GBK","UTF-8",$b);
           $msgType = "text";
           if ($c==$fromUsername)
           {
             $contentStr = "欢迎老朋友!";
           }else{
            $sql="INSERT INTO `welcome`(`id`,`user`) VALUES (NULL,'{$fromUsername}')";
            mysql_query($sql);
            $contentStr = "欢迎新朋友!";
           }

           $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);         
           echo $resultStr;
           mysql_close($db);
         }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;
    }
  }
}

?>

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

相关推荐