微信接口开发教程php,PHP对接微信公众平台消息接口开发流程教程_PHP教程

一、写好接口程序

在你的服务器上上传好一个接口程序文件,如http://www.yourdomain.com/weixin.php 内容如下:

复制代码 代码如下:

%s

0";

if(!empty( $keyword ))

{

$msgType = "text";

$contentStr = '你好啊,屌丝';

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

}else{

echo '咋不说哈呢';

}

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

}

}

}

?>

二、配置微信公众平台回复接口

设置回复接口,填好URL和Token(url填上面的http://www.yourdomain.com/weixin.php,token必须跟上面程序里面定义的Token一致)

2105203637-0.jpg?2014225141017

三、验证接口

用自己的个人微信关注下你的公众账号,给这个账号发一条消息过去,收到原样的消息返回,即验证成功了。

四、开始自定义回复

注释掉$wechatObj->valid(); 这行,同时去掉//$wechatObj->responseMsg();这行的注释。

你可以修改responseMsg函数里面的代码,根据用户的消息类型('text','image','location')和消息内容来回复用户不同的内容。

消息接口就可以使用了,发个消息试试看吧?

1.封装weixin.class.php

由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处理。

我们就考虑在这个基础上做一次封装,weixin.class.php,代码如下:

复制代码 代码如下:

msg['FromUserName']}]]>

msg['ToUserName']}]]>

{$CreateTime}

1

]>

%s

";

return sprintf($textTpl,$text,$FuncFlag);

}

//根据数组参数回复图文消息

public function makeNews($newsData=array())

{

$CreateTime = time();

$FuncFlag = $this->setFlag ? 1 : 0;

$newTplHeader = "msg['FromUserName']}]]>

msg['ToUserName']}]]>

{$CreateTime}

%s";

$newTplItem = "";

$newTplFoot = "

%s";

$Content = '';

$itemsCount = count($newsData['items']);

$itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条

if ($itemsCount) {

foreach ($newsData['items'] as $key => $item) {

if ($key<=9) {

$Content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']);

}

}

}

$header = sprintf($newTplHeader,$newsData['content'],$itemsCount);

$footer = sprintf($newTplFoot,$FuncFlag);

return $header . $Content . $footer;

}

public function reply($data)

{

if ($this->debug) {

$this->write_log($data);

}

echo $data;

}

public function valid()

{

if ($this->checkSignature()) {

if( $_SERVER['REQUEST_METHOD']=='GET' )

{

echo $_GET['echostr'];

exit;

}

}else{

write_log('认证失败');

exit;

}

}

private function checkSignature()

{

$signature = $_GET["signature"];

$timestamp = $_GET["timestamp"];

$nonce = $_GET["nonce"];

$tmpArr = array($this->token, $timestamp, $nonce);

sort($tmpArr);

$tmpStr = implode( $tmpArr );

$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){

return true;

}else{

return false;

}

}

private function write_log($log){

//这里是你记录调试信息的地方 请自行完善 以便中间调试

}

}

?>

2.调用weixin.class.php

把你的微信公众平台主接口文件(如前面定义的http://www.yourdomain.com/weixin.php)中,修改代码为:

复制代码 代码如下:

3.查询代码

还需要将数据库里面的查询结果格式化为特定的形式

复制代码 代码如下:

http://www.bkjia.com/PHPjc/745823.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/745823.htmlTechArticle一、写好接口程序 在你的服务器上上传好一个接口程序文件,如http://www.yourdomain.com/weixin.php 内容如下: 复制代码 代码如下: ?php define("TO...

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

相关文章

相关视频

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值