I try to do wechat api integration. By putting the file in public folder it work. Here is my code
<?php
traceHttp();
define('TOKEN', 'xxxxx');
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET['echostr'];
if($this->checkSignature()){
echo $echoStr;
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;
}
}
public function responseMsg(){
//get post data, May be due to the different environments
$postStr = file_get_contents('php://input');
//if (!empty($postStr)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
//libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
switch($postObj->MsgType){
case "event":
$this->_doEvent($postObj);
break;
case "text":
$this->_doText($postObj);
break;
case "image":
$this->_doImage($postObj);
break;
case "voice":
$this->_doVoice($postObj);
break;
case "music":
$this->_doMusic($postObj);
break;
case "location":
$this->_doLocation($postObj);
break;
default:
break;
}
}
Now I want to create all this function inside a controller. But I get empty value for file_get_contents('php://input')
. I even try to use $request->getContent()
& $request->all()
and both return empty value.
Anyone can advise me what is the problem? I'm been stuck to this problem for the whole day and I'm really appreciate any help. Thanks
Below is my laravel controller code:
public function authenticate(Request $request) {
$token = 'xxxxxx';
define("TOKEN", $token);
if(isset($_GET['echostr'])) {
$this->valid();
} else {
$content = $request->all();
}
}
public function valid() {
$echoStr = $_GET["echostr"];
if($this->checkSignature()) {
echo $echoStr;
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;
}
}