【发布时间】:2018-08-26 23:21:03
【问题描述】:
您好,我正在尝试使用 API。 但是我不断收到这个奇怪的错误,我已经搜索了互联网,但在尝试阅读该错误时似乎无法修复它。
我收到此错误:注意:尝试在第 22 行的 C:\xampp\htdocs\index.php 中获取非对象的属性
这里是 index.php 的第 17 到 28 行
$server = "localhost:8087";
$player = $_SESSION['username'];
$password = $_SESSION['password'];
$params = array("Command" => "AccountsPassword", "Player" => $player, ":", "PW" => $password);
$api = Poker_API($params);
if ($api -> Result != "Ok") die($api -> Error . "<br/>" . "Go to the homepage to try again.");
if ($api -> Verified != "Yes") die("Password is incorrect. Go to the homepage to try again.");
$params = array("Command" => "AccountsSessionKey", "Player" => $player);
$api = Poker_API($params);
if ($api -> Result != "Ok") die($api -> Error . "<br/>" . "Go to the homepage to try again.");
$key = $api -> SessionKey;
$src = $server . "/?LoginName=" . $player . "&SessionKey=" . $key;
这里是 api.php
<?php
$url = "localhost:8087/api"; // Put your API path here
$pw = "Removed"; // put your API password here
function Poker_API($params)
{
global $url, $pw;
$params['Password'] = $pw;
$params['JSON'] = 'Yes';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
if (curl_errno($curl)) $obj = (object) array('Result' => 'Error', 'Error' => curl_error($curl));
else if (empty($response)) $obj = (object) array('Result' => 'Error', 'Error' => 'Connection failed');
else $obj = json_decode($response, true);
curl_close($curl);
print_r($response);
//echo implode($params);
return $obj;
}
?>
【问题讨论】:
-
this
$obj = json_decode($response, true);返回关联的数组而不是对象。抛弃true,你会收到一个对象(除非有json错误) -
天哪,谢谢它有效!
标签: php arrays json object curl