简体   繁体   English

注意:在PHP 5.6中使用JSON时,尝试获取非对象错误的属性

[英]Notice: Trying to get property of non-object error when using JSON with PHP 5.6

I have PHP web page, but I have one problem with the code, I search here, got some similar answer, but sorry I do not understand how to fix my error. 我有PHP网页,但是代码有一个问题,我在这里搜索,得到了类似的答案,但是对不起,我不明白如何解决我的错误。 When I use MySQLI and PHP 7.1, then my web page work but there is some error, when I use MySQL and PHP 5.6, then my web page works perfect, except for this error, which is below. 当我使用MySQLI和PHP 7.1时,我的网页可以工作,但是有一些错误,当我使用MySQL和PHP 5.6时,我的网页可以正常工作,但以下错误除外。

this is my error: 这是我的错误:

Notice: Trying to get property of non-object in /var/www/html/lib/block_io.php on line 95

Notice: Trying to get property of non-object in /var/www/html/lib/block_io.php on line 95

Notice: Trying to get property of non-object in /var/www/html/lib/block_io.php on line 95

Fatal error: Uncaught exception 'Exception' with message 'Failed: ' in /var/www/html/lib/block_io.php:95 Stack trace: #0 /var/www/html/lib/block_io.php(53): BlockIo->_request('get_new_address', Array) #1 /var/www/html/block.php(20): BlockIo->__call('get_new_address', Array) #2 /var/www/html/block.php(20): BlockIo->get_new_address() #3 /var/www/html/deposit.php(76): include('/var/www/html/b...') #4 {main} thrown in /var/www/html/lib/block_io.php on line 95

this is a lines from the code 这是代码中的一行

81         // it's a GET method
82         if ($method == 'GET') { $url .= '&' . $addedData; }
83         curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
84         curl_setopt($ch, CURLOPT_URL, $url);
85         if ($method == 'POST')
86         { // this was a POST method
87         curl_setopt($ch, CURLOPT_POST, 1);
88         curl_setopt($ch, CURLOPT_POSTFIELDS, $addedData);
89         }
90         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
91         // Execute the cURL request
92         $result = curl_exec($ch);
93         curl_close($ch);
94         $json_result = json_decode($result);
95         if ($json_result->status != 'success') { throw new 
           Exception('Failed: ' . $json_result->data->error_message); }
96          // Spit back the response object or fail
97          return $result ? $json_result : false;        
98          }

I know that I can't ask good question, I don't know how to search answer here, I don't know how to use answer if I find something similar, but maybe someone can explain me, what I have wrong in this code and give some advice how to fix it 我知道我不能问一个好问题,我不知道如何在这里搜索答案,如果我找到类似的东西,我也不知道如何使用答案,但是也许有人可以向我解释,我在这里有什么错代码并提供一些建议以解决问题

EDIT 编辑

OK, I don't know if I do correct, but this var_dump I use here in my line 94 好的,我不知道我是否正确,但是我在第94行中使用的var_dump

94         $json_result = var_dump($result);

and then I get this error 然后我得到这个错误

bool(false) 
Notice: Trying to get property of non-object in /var/www/html/lib/block_io.php on line 95

Notice: Trying to get property of non-object in /var/www/html/lib/block_io.php on line 95

Notice: Trying to get property of non-object in /var/www/html/lib/block_io.php on line 95

Fatal error: Uncaught exception 'Exception' with message 'Failed: ' in /var/www/html/lib/block_io.php:95 Stack trace: #0 /var/www/html/lib/block_io.php(53): BlockIo->_request('get_new_address', Array) #1 /var/www/html/block.php(20): BlockIo->__call('get_new_address', Array) #2 /var/www/html/block.php(20): BlockIo->get_new_address() #3 /var/www/html/deposit.php(76): include('/var/www/html/b...') #4 {main} thrown in /var/www/html/lib/block_io.php on line 95

Ok, here is my full php file, maybe someone see mistake other line not the line 95, please help me, I don't know how to fix this error 好的,这是我完整的php文件,也许有人看到其他行而不是95行的错误,请帮助我,我不知道如何解决此错误

http://sandbox.onlinephpfunctions.com/code/29b428d4f224990f865e2a708fd188fd66016766 http://sandbox.onlinephpfunctions.com/code/29b428d4f224990f865e2a708fd188fd66016766

Try json_decode( $result, true ); 尝试json_decode( $result, true ); you need to tell the function it is an object 您需要告诉函数它是一个对象

json_decode returns true, false, null, an object or an array depending on the given json input. json_decode根据给定的json输入返回true,false,null,对象或数组。 See here . 看这里

$json_result->status != 'success' assumes that on object was returned. $json_result->status != 'success'假定返回了on对象。

In order to verify this, temporarily change line 95 like this: 为了验证这一点,请临时更改第95行,如下所示:

if ($json_result === true) {
    echo('TRUE');
}
elseif ($json_result === false) {
    echo('FALSE');
}
elseif ($json_result === null) {
    echo('NULL');
}
elseif (is_array($json_result) {
    echo('ARRAY');
    echo($json_result['status'];
}
else {
    echo('EXPECTED OBJECT');
    echo($json_result->status);
}

Or check the content of $result in line 92. 或者检查第92行中$ result的内容。

Then you see what json you actually have got and what decoded value you get from it. 然后,您将看到实际获得的json和从中获得的解码值。

As the var-dump shows, you seem to get false . 如var-dump所示,您似乎得到了false If false is not a valid return value sent by the service, then your curl call is most likely wrong. 如果false不是该服务发送的有效返回值,则您的curl调用很可能是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM