Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法检索文件获取内容(';php://input';)在laravel 5.5控制器中_Php_Laravel_Laravel 5.5_Wechat - Fatal编程技术网

无法检索文件获取内容(';php://input';)在laravel 5.5控制器中

无法检索文件获取内容(';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

我尝试做微信api集成。通过将文件放入公用文件夹,它就可以工作。这是我的密码

<?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;
    }
}