无法检索文件获取内容(';php://input';)在laravel 5.5控制器中
我尝试做微信api集成。通过将文件放入公用文件夹,它就可以工作。这是我的密码无法检索文件获取内容(';php://input';)在laravel 5.5控制器中,php,laravel,laravel-5.5,wechat,Php,Laravel,Laravel 5.5,Wechat,我尝试做微信api集成。通过将文件放入公用文件夹,它就可以工作。这是我的密码 <?php traceHttp(); define('TOKEN', 'xxxxx'); $wechatObj = new wechatCallbackapiTest(); if (isset($_GET['echostr'])) { $wechatObj->valid(); }else{ $wechatObj->responseMsg(); } class wechatCallb
<?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;
}
}
用于验证的微信api将使用GET请求,但用于发送消息的微信api将使用POST请求 由于my route.php route只是route::get,因此my
$request->getContent()
返回空
因此,与其使用Route::get('/wechat-api','WController@verify');代码>
我改为Route::any('/wechat-api','WController@verify');代码>
进行更改后,$request->getContent()
最终不再为空。$request->getContent()
应该包含php://input
您可以对其进行分析。但是,如果您错误配置了导致重定向的webhook URL(因为您的Web服务器强制执行尾部的/
或https重定向),则数据可能会丢失,因此请确保微信具有正确且准确的应调用URL。如果不是这样的话,看看一些微信SDK软件包是如何处理的:介意与否决票分享原因吗?@AlexBouma谢谢,我终于解决了这个问题。请参考我的答案。
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;
}
}