注意:试图获取非对象CURL结果的属性?

时间:2018-08-26 23:21:03

标签: php arrays json object curl

嗨,我正在尝试弄乱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 . "&amp;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;
    }

?>

2 个答案:

答案 0 :(得分:1)

注意不是错误。 当您没有错误时,可能会发生这种情况,因为您要在此行中返回数组:

else $obj = json_decode($response, true);

True作为第二个参数,强制此函数返回关联数组。因此,您没有要呼叫的对象。

答案 1 :(得分:0)

尝试添加以下两行:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

对我有用。