首页 > 解决方案 > PHP:它想要一个“对象”,所以我给它和对象,然后它错误地说它想要一个“数组”,给出了什么?

问题描述

我很困惑。使用PHP 包装器使用 Cardano API (v1) 。

createNewTransaction($source, $destination, $spendingPassword)函数需要数组形式的参数:

// source of funds (array)
    $source = array(
                'accountIndex'  => $wallet_idx,
                'walletId'      => $wallet_id
            );

    // destination for funds (array)
    $destination = array(
                'address' => $banker,
                'amount'  => $lovelace
            );

    // spending pass
    $spendingPassword = get_user_meta('1', 'spending_pass', true);

    // transaction
    $client->createNewTransaction($source, $destination, $spendingPassword);

然后我从 API 返回了这个错误:

Array ( [status] => error [diagnostic] => Array ( [validationError] => Error in $: When parsing the constructor Payment of type Cardano.Wallet.API.V1.Types.Payment 预期对象但得到 Array.) [消息] => JSONValidationFailed )

因此,我查找了如何将数组转换为对象,从而解决我的问题。所以我这样做了:

   $o_source = (object) $source;
   $o_destination = (object) $destination;

然后将其发送回 API 以查看事务是否会通过,但我收到另一个错误:

致命错误:未捕获的 TypeError:传递给 Cardano::createNewTransaction() 的参数 1 必须是数组类型,给定对象...

所以,现在我很困惑,我以为我最初给它发送了一个数组,但后来它说它想要一个对象。我不明白!

请帮忙!

标签: phparraysobject

解决方案


链接库中的代码如下所示:

public function createNewTransaction(
    array $source,
    array $destination,
    string $spendingPassword
): array {
    $groupPolicy = 'OptimizeForSecurity';
    return self::jsonDecode($this->post(
        '/api/v1/transactions' .
        '?source=' . $source .
        '?destination=' . $destination .
        '?groupingPolicy=' . $groupPolicy .
        '?spendingPassword=' . $spendingPassword
    ), true);
}

如果您运行此代码,则$sourceand$destination将从数组转换为字符串“Array”。这就是 API 所抱怨的,它可能期望字符串“Array”以外的其他内容作为source参数的值。

我建议你寻找另一个库,或者自己实现 API 调用,因为这个库在当前状态下没有机会正常工作。


推荐阅读