【问题标题】:PHP: Getting 2 warning & 6 notices [closed]PHP:收到 2 个警告和 6 个通知 [关闭]
【发布时间】:2016-03-01 19:39:00
【问题描述】:

我需要 2 个警告和 6 个通知方面的帮助。

注意:试图获取非对象的属性 在第 11 行

注意:试图获取非对象的属性 第 28 行

注意:试图获取非对象的属性 第 29 行

注意:试图获取非对象的属性 第 30 行

注意:试图获取非对象的属性 第 31 行

注意:试图获取非对象的属性 第 33 行

警告:PDOStatement::execute(): SQLSTATE[23000]: 完整性 违反约束:1048 列“steamID”不能为空 第 34 行

我已经尝试修复代码 4 个小时了,但我就是不明白到底发生了什么......

<?php
require_once "../config.php";
require_once "../db.php";
if(!isset($_GET['secret']) || $_GET['secret'] != $config['bot_key_add']){
    die("Access denied");
    return;
}

$json = json_decode(file_get_contents("php://input"), false, 512, JSON_BIGINT_AS_STRING);

if((int)$json->userID ==)       
    return;

$weapon = urlencode($json->weapon);
$link = "http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=".$weapon;
$steam_price = file_get_contents($link);
$item_price = json_decode($steam_price, false, 512, JSON_BIGINT_AS_STRING);
if(empty($item_price) || $item_price->success == false || empty($item_price->median_price))
{
    $price = 400;
}
else 
{
    $price = $item_price->median_price;
    $price = str_replace("&#36;" , "", $price);
    $price = str_replace("$" , "", $price);
    $price = round(str_replace(",",".",$price), 2);
}
$saveWeapon = $pdo->prepare("INSERT INTO `{$config['db_prefix']}users_items` (`steamID`, `classid`, `assetid`, `weapon_name`, `price`, `float`) VALUES (:steamId , :classid , :assetid , :weaponName , :price, :float)");

$saveWeapon->execute([  ":steamId"      => $json->userID,
                        ":classid"      => $json->classid,
                        ":assetid"      => $json->assetid,
                        ":weaponName"   => $json->weapon,
                        ":price"        => $price,
                        ":float"        => $json->color
]);
?>

【问题讨论】:

  • 您应该在此处发布警告,而不是链接到它的图像。
  • 看起来 json_decode 返回数组而不是对象,但您正试图将属性作为对象访问。 stackoverflow.com/questions/21974951/… 你能用var_dump($json) 的输出更新问题吗?
  • $json = NULL /3chars
  • (int)$json-&gt;userID ==)

标签: php mysql json


【解决方案1】:

您的 json 解码似乎失败了。您收到通知的行是您尝试将 $json 视为对象的地方。

根据 http://php.net/manual/en/function.json-decode.php ,当 json 解码失败时,它返回 null,您不能将其视为对象。

我建议在 json 解码后使用 http://php.net/manual/en/function.json-last-error-msg.php 进行一些错误检查

if($json === null) {
    throw new Exception(json_last_error_msg());
}

请注意,如果您使用的是 5.5.0 之前的 php,则需要定义该函数(来自该函数参考)

if (!function_exists('json_last_error_msg')) {
    function json_last_error_msg() {
        static $ERRORS = array(
            JSON_ERROR_NONE => 'No error',
            JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
            JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
            JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
            JSON_ERROR_SYNTAX => 'Syntax error',
            JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
        );

        $error = json_last_error();
        return isset($ERRORS[$error]) ? $ERRORS[$error] : 'Unknown error';
    }
}

【讨论】:

    【解决方案2】:

    基本上您的 file_get_contents("php://input") 不会返回文件对象,因此由于缺少文件对象而导致 rest 失败(所有警告)。因此“file_get_contents”无法打开正确的文件,并且 Steam 以错误方式回答。休息是一个后续错误。

    您可以通过检查和清理脚本的输入来避免这种情况,如果检查失败则中止。

    【讨论】:

    • 谢谢,那我只需要启动“bot”就可以了。
    【解决方案3】:

    您在尝试 file_get_contents() 时收到 400 Bad Request:

    $link = "http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=".$weapon;
    $steam_price = file_get_contents($link);
    

    然后您尝试对此响应进行 json_decode 并且您的 $item_price 将为空。 第二个警告,因为您的 INSERT 尝试添加包含所有空字段但 steamID 不能为空的行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 2020-02-15
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多