Skip to content

Commit

Permalink
Weixin API SDK
Browse files Browse the repository at this point in the history
Weixin API SDK
  • Loading branch information
xu xianhua committed Jan 2, 2013
0 parents commit cf151ca
Show file tree
Hide file tree
Showing 5 changed files with 357 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
163 changes: 163 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#################
## Eclipse
#################

*.pydevproject
.project
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath


#################
## Visual Studio
#################

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results
[Dd]ebug/
[Rr]elease/
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.vspscc
.builds
*.dotCover

## TODO: If you have NuGet Package Restore enabled, uncomment this
#packages/

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf

# Visual Studio profiler
*.psess
*.vsp

# ReSharper is a .NET coding add-in
_ReSharper*

# Installshield output folder
[Ee]xpress

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish

# Others
[Bb]in
[Oo]bj
sql
TestResults
*.Cache
ClientBin
stylecop.*
~$*
*.dbmdl
Generated_Code #added for RIA/Silverlight projects

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML



############
## Windows
############

# Windows image file caches
Thumbs.db

# Folder config file
Desktop.ini


#############
## Python
#############

*.py[co]

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

#Translations
*.mo

#Mr Developer
.mr.developer.cfg

# Mac crap
.DS_Store
54 changes: 54 additions & 0 deletions demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* 微信 公众平台消息接口 SDK DEMO
* @author xhxu [email protected]/QQ:7844577
* @version 1.0.20130102
*/
error_reporting(E_ALL & ~E_NOTICE);
date_default_timezone_set("Asia/Shanghai");

require_once 'weixin.class.php';

define('TOKEN', 'YourToken'); //微信公众平台自定义接口处设置的 Token
define('DEBUG', true); //是否调试模式 true/false (开启调试将会把收发的信息写入文件)

$weixin = new weixin(TOKEN,DEBUG);

$weixin->valid();
$weixin->getMsg();
$type = $weixin->msgtype;

if ($type==='text') { //文本信息
if ($weixin->msg['Content']=='Hello2BizUser') {
//关注成功后的信息
$note = '你已经成功关注该公众账号';
}else{
$note = '你好,你发的信息是:'.$weixin->msg['Content'];
}
$reply = $weixin->makeText($note);
}elseif ($type==='location') {
$note = '您的位置在: '.$weixin->msg['Label'].'坐标是: X:'.$weixin->msg['Location_X'].' Y:'.$weixin->msg['Location_Y'];
$reply = $weixin->makeText($note);
}elseif ($type==='image') {
$news = array(
'content' =>'这个是图文消息',
'itemsCount' => 2 //内容条数最大10
);
$news['items'] = array(
array(
'title' => '微信 公众平台消息接口 SDK',
'description' => '微信 公众平台消息接口SDK 说明',
'picurl' => 'http://www.xhxu.cn/weixin/sdk.jpg', //图片地址为接口域名下图片
'url' => 'http://www.xhxu.cn'
),
array(
'title' => '微信 公众平台消息接口 SDK',
'description' => '微信 公众平台消息接口 SDK 说明2',
'picurl' => 'http://www.xhxu.cn/weixin/sdk.jpg',
'url' => 'http://www.xhxu.cn'
)
);
$reply = $weixin->makeNews($news);
}
//输出
$weixin->reply($reply);
Binary file added sdk.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 118 additions & 0 deletions weixin.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* 微信 公众平台消息接口 SDK
* @author xhxu [email protected]/QQ:7844577
* @version 1.0.20130102
*/
class Weixin
{
public $token = '';
public $debug = false;
public $flag = false;
public $msgtype = 'text'; //('text','image','location')
Public $msg = array();
Public $version = '1.0.20130102';

public function __construct($token,$debug)
{
$this->token = $token;
$this->debug = $debug;
}
public function setFlag()
{
$this->flag = true;
}
public function getMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if ($this->debug) {
file_put_contents('./log.txt', $postStr."\n",FILE_APPEND);
}
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->flag ? 1 : 0;
$textTpl = "<xml>
<ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
<FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
<CreateTime>{$CreateTime}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%s</FuncFlag>
</xml>";
return sprintf($textTpl,$text,$FuncFlag);
}
public function makeNews($newsData=array())
{
$CreateTime = time();
$FuncFlag = $this->flag ? 1 : 0;
$newTplHeader = "<xml>
<ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
<FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
<CreateTime>{$time}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[%s]]></Content>
<ArticleCount>%s</ArticleCount><Articles>";
$newTplItem = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$newTplFoot = "</Articles>
<FuncFlag>%s</FuncFlag>
</xml>";
$Content = '';
if (is_array($newsData['items'])) {
foreach ($newsData['items'] as $item) {
$Content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']);
}
}
$header = sprintf($newTplHeader,$newsData['content'],$newsData['itemsCount']);
$footer = sprintf($newTplFoot,$FuncFlag);
return $header . $Content . $footer;
}
public function reply($data)
{
if ($this->debug) {
file_put_contents('./reply.txt', $data."\n",FILE_APPEND);
}
echo $data;
}
public function valid()
{
if ($this->checkSignature()) {
if( $_SERVER['REQUEST_METHOD']=='GET' )
{
echo $_GET['echostr'];
exit;
}
}else{
file_put_contents('./log.txt', 'valid fild'."\n",FILE_APPEND);
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;
}
}
}

0 comments on commit cf151ca

Please sign in to comment.