php - 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 必须是数组类型,给定对象...
所以,现在我很困惑,我以为我最初给它发送了一个数组,但后来它说它想要一个对象。我不明白!
请帮忙!
解决方案
链接库中的代码如下所示:
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);
}
如果您运行此代码,则$source
and$destination
将从数组转换为字符串“Array”。这就是 API 所抱怨的,它可能期望字符串“Array”以外的其他内容作为source
参数的值。
我建议你寻找另一个库,或者自己实现 API 调用,因为这个库在当前状态下没有机会正常工作。
推荐阅读
- javascript - 即使单词之前或之后有文本,如何读取字符串?
- react-native - 如何在自己的文件中正确实现 React Native 中的自定义组件?
- javascript - 使用 Javascript 通过 JSON 查询类别
- python - 如何根据其他索引从其他数据框的列中获取值?
- angular - Angular HttpRequest 不发送参数
- python - 使用过滤器查询 ADH
- android - 从 sd 卡加载 gif 图像
- amazon-web-services - 已使用 NoSQL Workbench 在 Cloud DynamoDB 数据模型中更新
- authentication - 克服 SSO 和静默登录的 ITP 或第三方 Cookie 问题
- recursion - 带有递归的 F# sum 类型?