search
Homephp教程php手册微信公众平台开发实例(琴岛学院校园之声) PHP开发 代码挂载SAE

欢迎关注我的微信 公众 账号“ APP每日推荐 ” 推荐一些精彩手机应用,互联网行业资讯~ !--题外话 :做这样一个系列的原因在于,这个微信 公众 平台 算的上是我解除编程一来 开发 的第一个有实际用途的东西,在这之中有很多体会,不敢说讲给大家听,只是同大

欢迎关注我的微信公众账号“APP每日推荐

微信公众平台开发实例(琴岛学院校园之声) PHP开发 代码挂载SAE

推荐一些精彩手机应用,互联网行业资讯~




最终制作效果请见:http://blog.csdn.net/c123853648/article/details/11095043

之前我们做了一系列的准备工作,这期开始我们正式进入编码工作。

首先我们需要在代码中修改一个地方,上次在验证的时候我们添加了这样一行代码

$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

现在验证已经完成我们需要这样修改一下,修改为
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();

下面是官方提供的demo
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 = "
							
							
							%s
							
							
							0
							";             
				if(!empty( $keyword ))
                {
              		$msgType = "text";
                	$contentStr = "Welcome to wechat world!";
                	$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;
		}
	}
}

?>
前23行和61行之后的开发时并没有太多关注,这里我就不做分析了,日后研究了再写吧。

我们从responseMsg()这个方法开始说吧。

$postStr变量接受用户提交的信息(xml格式),之后通过调用simplexml_load_string() 函数把 XML 字符串载入对象中。

微信公众平台开发实例(琴岛学院校园之声) PHP开发 代码挂载SAE

在得到了$postObj这个变量后,通过

 <span>这种方式获取到用户传来的各种数据,并存储在相应的变量中相应的信息格式参照消息接口指南:

<p>http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97</p>
<p>$textTpl这个变量用于存储各种回复信息类型的结构,可以在消息接口指南中查到,这里可以根据自己的需求添加不同的信息类型结构的变量</p>
<p>$contentStr这个变量对应着回复文本信息的文本内容</p>
<p>sprintf函数的作用可以说是将需要回复的信息,添加到$textTpl定义的“结构”中,springf函数使用方法如下:</p>

<pre class="brush:php;toolbar:false">$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
微信公众平台开发实例(琴岛学院校园之声) PHP开发 代码挂载SAE

在后面的开发过程中,基本上是根据【获取用户发送的内容】->【对内容的处理】->【通过编写相关方法,将内容在数据库中做相关操作】->【返回数据库操作的结果】->【填充$textTpl“结构”中的变量】->【输出结果】



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.