Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #33, fix HTTP_RAW_POST_DATA not set error #34

Merged
merged 2 commits into from
Dec 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/Wechat.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,39 @@ class Wechat {
*/
private $encrypted = false;

/**
* Store post data from wechat server
*
* @var string
*/
private $postStr;

/**
* 初始化,判断此次请求是否为验证请求,并以数组形式保存
*
* @param string $token 验证信息
* @param boolean $debug 调试模式,默认为关闭
*/
public function __construct($config=array('token'=>'', 'aeskey'=>'', 'appid'=>'', 'debug' => FALSE)) {

$token = $config['token'];
$aeskey = $config['aeskey'];
$appid = $config['appid'];
$debug = $config['debug'];

if (!$this->validateSignature($token)) {
exit('签名验证失败');
}

if ($this->isValidateIncomingConn()) {
// 网址接入验证
exit($_GET['echostr']);
}

if (!isset($GLOBALS['HTTP_RAW_POST_DATA'])) {

if ($_SERVER['REQUEST_METHOD'] == "POST") {
$this->postStr = file_get_contents("php://input");
}
if (!isset($this->postStr)) {
exit('缺少数据');
}

Expand All @@ -85,15 +95,15 @@ private function savePostData() {
$xml = '';

if ($this->encrypted) {
$errCode = $this->msgCryptor->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $GLOBALS['HTTP_RAW_POST_DATA'], $xml);
$errCode = $this->msgCryptor->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postStr, $xml);

if ($errCode != 0) exit($errCode);

} else {
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
$xml = $this->postStr;
}

$xml = (array) simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$xml = (array) simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

$this->request = array_change_key_case($xml, CASE_LOWER);
// 将数组键名转换为小写,提高健壮性,减少因大小写不同而出现的问题
Expand All @@ -118,7 +128,7 @@ private function validateSignature($token) {
if ( ! (isset($_GET['signature']) && isset($_GET['timestamp']) && isset($_GET['nonce']))) {
return FALSE;
}

$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
Expand Down Expand Up @@ -195,28 +205,28 @@ protected function onLink() {}
* 收到自定义菜单消息时触发,用于子类重写
*
* @return void
*/
*/
protected function onClick() {}

/**
* 收到地理位置事件消息时触发,用于子类重写
*
* @return void
*/
*/
protected function onEventLocation() {}

/**
* 收到语音消息时触发,用于子类重写
*
* @return void
*/
*/
protected function onVoice() {}

/**
* 扫描二维码时触发,用于子类重写
*
* @return void
*/
*/
protected function onScan() {}

/**
Expand Down
25 changes: 12 additions & 13 deletions src/errorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
*/
class ErrorCode
{
public $OK = 0;
public $ValidateSignatureError = -40001;
public $ParseXmlError = -40002;
public $ComputeSignatureError = -40003;
public $IllegalAesKey = -40004;
public $ValidateAppidError = -40005;
public $EncryptAESError = -40006;
public $DecryptAESError = -40007;
public $IllegalBuffer = -40008;
public $EncodeBase64Error = -40009;
public $DecodeBase64Error = -40010;
public $GenReturnXmlError = -40011;
static $OK = 0;
static $ValidateSignatureError = -40001;
static $ParseXmlError = -40002;
static $ComputeSignatureError = -40003;
static $IllegalAesKey = -40004;
static $ValidateAppidError = -40005;
static $EncryptAESError = -40006;
static $DecryptAESError = -40007;
static $IllegalBuffer = -40008;
static $EncodeBase64Error = -40009;
static $DecodeBase64Error = -40010;
static $GenReturnXmlError = -40011;
}