微信接口测试号 php代码,模拟测试微信接口暨微信开发试验代码

要成为微信公众号(订阅号或服务号)的开发者,需要首先验证接口,这个可以在登录微信https://mp.weixin.qq.com后台后设置。但是我嫌麻烦,于是开发个接口类,包含验证函数(还有回复文本信息和图文信息的功能)。其实接口验证在成为开发者之后就没用了。

上代码,微信基类:weixin.class.php

<>

class weixin

{

public $token = '';//token

public $debug = false;//是否debug的状态标示,方便我们在调试的时候记录一些中间数据

public $setflag = false;

public $msgtype = 'text'; //('text','image','location')

public $msg = array();

public function __construct($token,$debug)

{

$this->token = $token;

$this->debug = $debug;

}

//获得用户发过来的消息(消息内容和消息类型 )

public function getmsg()

{

$poststr = $globals["http_raw_post_data"];

if ($this->debug)

{

$this->write_log($poststr);

}

if (!empty($poststr))

{

$this->msg = (array)simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);

$this->msgtype = strtolower($this->msg['msgtype']);

}

}

//回复文本消息

public function maketext($text='')

{

$createtime = time();

$funcflag = $this->setflag ? 1 : 0;

$texttpl = "msg['fromusername']}]]>

msg['tousername']}]]>

{$createtime}

%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);

$itemscount = $itemscount < 10="" $itemscount="" :="">

if ($itemscount)

{

foreach ($newsdata as $key => $item)

{

if ($key<>

{

$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)

{

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

}

}

?>

微信接口的代码:weixin.php

<>

header("content-type: text/html;charset=utf-8");

include_once('weixin.class.php'); //引用刚定义的微信消息处理类

define("token", "itwatch"); //mmhelper

define('debug', false);

$weixin = new weixin(token, debug); //实例化

//$weixin->valid();

$weixin->getmsg();

$type = $weixin->msgtype; //消息类型

$username = $weixin->msg['fromusername']; //哪个用户给你发的消息,这个$username是微信之后的,但是每个用户都是一一对应的

if ($type==='text')

{

//if ($weixin->msg['content']=='hello2bizuser')

if ($weixin->msg['content']=='你好')

{ //微信用户第一次关注你的账号的时候,你的公众账号就会受到一条内容为'hello2bizuser'的消息

$reply = $weixin->maketext('欢迎你关注网眼视界威信公众平台');

}

else

{ //这里就是用户输入了文本信息

$keyword = $weixin->msg['content']; //用户的文本消息内容

//include_once("chaxun.php"); //文本消息 调用查询程序

//$chaxun= new chaxun(debug, $keyword, $username);

//$results['items'] =$chaxun->search(); //查询的代码

//$reply = $weixin->makenews($results);

$arraycon = array(

array(

"title"=>"电脑学习网",

"description"=>"十万个为什么-电脑学习网",

"picurl"=>"https://www.veryphp.cn/datas/userfiles/8bd108c8a01a892d129c52484ef97a0d/images/website13.jpg",

"url"=>"https://www.why100000.com/"

),

array(

"title"=>"非常php学习网",

"description"=>"大型php学习分享社区",

"picurl"=>"https://www.veryphp.cn/datas/userfiles/8bd108c8a01a892d129c52484ef97a0d/images/php01.jpg",

"url"=>"https://www.veryphp.cn/"

)

);

$results = $arraycon;

$reply = $weixin->makenews($results);

}

}

elseif ($type==='location')

{

//用户发送的是位置信息 稍后处理

}

elseif ($type==='image')

{

//用户发送的是图片 稍后处理

}elseif ($type==='voice')

{

//用户发送的是声音 稍后处理

}

//

$weixin->reply($reply);

?>

验证微信接口的代码,用 curl 函数完成,需要打开php的 curl 扩展。把 weixin.php 文件中的 //$weixin->valid(); 一句的注释去掉即可验证,完了把这句注释掉即可。

<>

//header("content-type: text/html;charset=utf-8");

//准备数据

define('token', 'itwatch');//自己定义的token 就是个通信的私钥

$echostr = '返回此数据表明正确。';

$timestamp = (string)time(); //本身为整数,必须转换为字符串

$nonce = 'my-nonce';

$signature = signature(token, $timestamp, $nonce);

function signature($token, $timestamp, $nonce)

{

$tmparr = array($token, $timestamp, $nonce);

sort($tmparr);

$tmpstr = implode($tmparr);

$tmpstr = sha1($tmpstr);

return $tmpstr;

}

//提交

$post_data = array(

"signature=$signature",

"timestamp=$timestamp",

"nonce=$nonce",

"echostr=$echostr"

);

$post_data = implode('&',$post_data);

$url='https://www.veryphp.cn/tools/weixin/weixin.php';

$ch = curl_init();

curl_setopt($ch, curlopt_url, $url.'?'.$post_data); //模拟get方法

ob_start();

curl_exec($ch);

$result = ob_get_contents();

ob_end_clean();

echo $result;

?>

以上的核心代码是 weixin.class.php 和 weixin.php 两个文件,是我调试成功的,已经部署在我的服务器上了。你要测试的话,用手机微信收听微信号:itwatch,然后输入“你好”,会返回字符串:欢迎你关注网眼视界威信公众平台。随便输入,会打开一个图文消息。

好吧,我承认以上代码写的非常凌乱,因为我十分瞌睡了, 要睡觉了。但以上代码确实是能工作的,是典型的原理实现性测试代码。希望给微信开发者提供个思路,看明白之后可以结合写一个功能完善的微信信息后台管理程序。。

有微信服务号的,可以在此基础上开发个菜单,然后调用仿照以上代码开发的消息回复。其实很简单。

这才是真正的网络通信程序,比你写企业站,把数据输进去,再按顺序检索出来分页显示,要有意思的多。

网眼-张庆

2013-12-3 ?

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值