• 技术文章 >后端开发 >php教程

    Call to a member function valid() on boolean这是出了什么错误?

    2016-06-06 20:31:53原创2303

    我把一段代码从thinkphp 3.1 移植到 thinkphp 3.2 ,然后调整的时候出现了这么一个错误,找了好多都没找到。

    public function init() {
            $config = M ( "Wxconfig" )->where ( array (
                    "id" => "1" 
            ) )->find ();
            $options = array (
                    'token' => $config ["token"], // 填写你设定的key
                    'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
                    'appid' => $config ["appid"], // 填写高级调用功能的app id
                    'appsecret' => $config ["appsecret"], // 填写高级调用功能的密钥
                    );
            $weObj = A('Api/Wechat ( $options )');
            return $weObj;
        }
        public function index() {
            $weObj = $this->init();
            $weObj -> valid ();
    

    下面是Api/Wechat中的那个 valid方法:

    /**
         * For weixin server validation
         * @param bool $return 是否返回
         */
        public function valid($return=false)
        {
            $encryptStr="";
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                $postStr = file_get_contents("php://input");
                $array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"]: '';
                if ($this->encrypt_type == 'aes') { //aes加密
                    $this->log($postStr);
                    $encryptStr = $array['Encrypt'];
                    $pc = new Prpcrypt($this->encodingAesKey);
                    $array = $pc->decrypt($encryptStr,$this->appid);
                    if (!isset($array[0]) || ($array[0] != 0)) {
                        if (!$return) {
                            die('decrypt error!');
                        } else {
                            return false;
                        }
                    }
                    $this->postxml = $array[1];
                    if (!$this->appid)
                        $this->appid = $array[2];//为了没有appid的订阅号。
                } else {
                    $this->postxml = $postStr;
                }
            } elseif (isset($_GET["echostr"])) {
                $echoStr = $_GET["echostr"];
                if ($return) {
                    if ($this->checkSignature())
                        return $echoStr;
                    else
                        return false;
                } else {
                    if ($this->checkSignature())
                        die($echoStr);
                    else
                        die('no access');
                }
            }
    
            if (!$this->checkSignature($encryptStr)) {
                if ($return)
                    return false;
                else
                    die('no access');
            }
            return true;
        }
    

    这是什么问题TAT

    回复内容:

    我把一段代码从thinkphp 3.1 移植到 thinkphp 3.2 ,然后调整的时候出现了这么一个错误,找了好多都没找到。

    public function init() {
            $config = M ( "Wxconfig" )->where ( array (
                    "id" => "1" 
            ) )->find ();
            $options = array (
                    'token' => $config ["token"], // 填写你设定的key
                    'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
                    'appid' => $config ["appid"], // 填写高级调用功能的app id
                    'appsecret' => $config ["appsecret"], // 填写高级调用功能的密钥
                    );
            $weObj = A('Api/Wechat ( $options )');
            return $weObj;
        }
        public function index() {
            $weObj = $this->init();
            $weObj -> valid ();
    

    下面是Api/Wechat中的那个 valid方法:

    /**
         * For weixin server validation
         * @param bool $return 是否返回
         */
        public function valid($return=false)
        {
            $encryptStr="";
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                $postStr = file_get_contents("php://input");
                $array = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $this->encrypt_type = isset($_GET["encrypt_type"]) ? $_GET["encrypt_type"]: '';
                if ($this->encrypt_type == 'aes') { //aes加密
                    $this->log($postStr);
                    $encryptStr = $array['Encrypt'];
                    $pc = new Prpcrypt($this->encodingAesKey);
                    $array = $pc->decrypt($encryptStr,$this->appid);
                    if (!isset($array[0]) || ($array[0] != 0)) {
                        if (!$return) {
                            die('decrypt error!');
                        } else {
                            return false;
                        }
                    }
                    $this->postxml = $array[1];
                    if (!$this->appid)
                        $this->appid = $array[2];//为了没有appid的订阅号。
                } else {
                    $this->postxml = $postStr;
                }
            } elseif (isset($_GET["echostr"])) {
                $echoStr = $_GET["echostr"];
                if ($return) {
                    if ($this->checkSignature())
                        return $echoStr;
                    else
                        return false;
                } else {
                    if ($this->checkSignature())
                        die($echoStr);
                    else
                        die('no access');
                }
            }
    
            if (!$this->checkSignature($encryptStr)) {
                if ($return)
                    return false;
                else
                    die('no access');
            }
            return true;
        }
    

    这是什么问题TAT

    $weObj = A('Api/Wechat ( $options )');

    Thinkphp A 函数不是这样调用吧

    意思是说现在版本的TP的init方法在你这里的代码中没有返回对象,而是返回了布尔值,很可能是false;

    跟踪一下3.2版本的A方法:

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php thinkphp
    上一篇:PHP的<embed>调用问题问题 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 一文详解PHP用流方式实现下载文件(附代码示例)• PHP反序列化入门总结(小白必看)• PHP原生类的总结分享• 聊聊PHP escapeshellarg函数使用的中文问题• 分享PHP函数使用小工具(附代码示例)
    1/1

    PHP中文网