Php Wechat Public Number Development

Posted on

In this paper, we share the specific code of PHP Wechat Public Number Development for your reference. The details are as follows.

Running through three passes

  • Impode (“,”, array) Array Variable String Array cannot be saved to the database
  • Explode (“,””, str) string variable array
  • Array_rand ($arr, n) randomly takes out n single items in an array

The core code is as follows:

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;
    $customevent = $postObj->Event;
    $latitude = $postObj->Location_X;
    $longitude = $postObj->Location_Y;
    $keyword = trim($postObj->Content);
    $time = time();
    $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[%s]]></MsgType>
        <Content>%s</Content>
        <FuncFlag>0</FuncFlag>
       </xml>"; 
    include("coon.php");   
    Array = array ("Hebei"=> "Shijiazhuang", "Shanxi"=> "Taiyuan", "Guangdong"=> "Guangzhou", "Heilongjiang"= "Harbin", "Guangxi"=> "Nanning", "Zhejiang"= "Hangzhou", "Jiangsu"=> "Nanjing", "Shandong"= "Jinan", "Henan"= "Zhengzhou");
    if($keyword=="8")
    {
     $a=array_rand($array,3);
     $contentStr= "Question 1". $a [0]. "Where is the provincial capital? ";
     $a=implode(",",$a); 
     $sql="SELECT * FROM `menu2` where `user`= '{$fromUsername}'";
     $query=mysql_query($sql);
     $rm=mysql_fetch_array($query); 
     $user=$rm['user'];
     if (empty($user))
     {
      $sql="INSERT INTO `menu2` (`id` ,`user` ,`sec`,`answer`,`num`)VALUES (NULL , '{$fromUsername}', '8','{$a}','0')";
      mysql_query($sql);
     } 
     else
     {
      $sql="UPDATE `menu2` SET `sec`='8',`answer`='{$a}', `num`='0' where `user`= '{$fromUsername}'";
      mysql_query($sql);
     }
    }
    else
    {
     $sql="SELECT * FROM `menu2` where `user`= '{$fromUsername}'";
     $query=mysql_query($sql);
     $rm=mysql_fetch_array($query);
     $sec=$rm['sec'];
     $answer=$rm['answer'];
     $answer=explode(",",$answer);

     $num=$rm['num'];  
     if($sec=="8")
     {

      if($num=="0")
      {

       if($keyword==$array[$answer[0]])
       {
        $contentStr= "Congratulations, you're right.  Where is the provincial capital? ";
        $sql="UPDATE `menu2` SET `num`='1' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
       else
       {
        $contentStr= "I'm sorry, you're wrong. Come back and play. ";
        $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
      } 
      elseif($num=="1")
      {
       if($keyword==$array[$answer[1]])
       {
        $contentStr= "Congratulations, you're right.  Where is the provincial capital? ";
        $sql="UPDATE `menu2` SET `num`='2' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
       else
       {
        $contentStr= "I'm sorry, try again and play again. ";
        $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
        mysql_query($sql);
       }
      }
      elseif($num=="2")
      {
       if($keyword==$array[$answer[2]]){
       $contentStr= "Congratulations, three passes in a row! ";
       $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
       mysql_query($sql);
      }
      else
      {
       $contentStr= "I'm sorry, it's only one step short. Let's play again. ";
       $sql="UPDATE `menu2` SET `sec`='' where `user`= '{$fromUsername}'";
       mysql_query($sql);
      }
     }
    } 
    else 
    {
     $contentStr= "Please enter 8 first";
    }
   }
   $msgType="text";
   $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
   echo $resultStr;

  }
 }

The menu2.sql database code is as follows:

-- phpMyAdmin SQL Dump
-- version 3.3.8.1
-- http://www.phpmyadmin.net
--
Host: w.rdc.sae.sina.com.cn:3307
- Date of Generation: 24:10, 26 August 2018
Server version: 5.6.23
- PHP version: 5.3.3

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
- Database: `app_dq095`
--

-- --------------------------------------------------------

--
--The structure of the table `menu2'`
--

CREATE TABLE IF NOT EXISTS `menu2` (
 `id` int(3) NOT NULL AUTO_INCREMENT,
 `user` varchar(255) NOT NULL,
 `sec` int(1) NOT NULL,
 `answer` varchar(10) NOT NULL,
 `num` int(4) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
- Data `menu2'in the dump table`
--

INSERT INTO `menu2` (`id`, `user`, `sec`, `answer`, `num`) VALUES
(1,'ooD-ow-C8ZigAihscJN54pAWpu2g', 8,'Guangdong, Jiangsu, Shandong', 1')
(2,'ooD-ow-C8ZigAihscJN54pAWpu2g', 8,'Guangdong, Jiangsu, Shandong', 1);

The coon.php connection database code is as follows:

<?php


  // User name: $user
  // Encryption: $pwd
  // Main Library Domain Name: $host
  // Slave Library Domain Name: SAE_MYSQL_HOST_S
  // End/mouth: $port
  // Database name: $dbname

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

  /* Then call mysql_connect() to connect to the server*/
  // Connected main storehouse
  $db = mysql_connect($host,$user,$pwd);

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

  /* Call mysql_select_db() immediately after successful connection to select the database to connect to*/
  if (!mysql_select_db($dbname)) {
   die("Select Database Failed: " . mysql_error($db));
  }
  mysql_query("set names utf-8",$db);

  /* Now that the connection has been fully established, the current database can be operated accordingly.*/
  /*!!! Note that mysql_select_db can no longer be called through this connection to switch to other databases!!! * /
  /* If you need to reconnect to another database, start another connection using mysql_connect+mysql_select_db*/

  /**
    * Next you can use other standard PHP MySQL function operations for database operations
  */

The overall code for index.php is as follows:

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

//define your token
define
("TOKEN", "weixin");
$wechatObj
= new wechatCallbackapiTest();
$wechatObj
->responseMsg();

class wechatCallbackapiTest
{
public function valid

Leave a Reply

Your email address will not be published.