Php 如何向curl函数发送正确类型的对象或数组
我对cURL一无所知,这对我来说完全是希腊语,我的PHP技能有限。我依靠别人的技能让我的生活更轻松,当事情没有按预期的方式进行时,我会感到非常沮丧 让我们从我的输入开始:Php 如何向curl函数发送正确类型的对象或数组,php,arrays,curl,Php,Arrays,Curl,我对cURL一无所知,这对我来说完全是希腊语,我的PHP技能有限。我依靠别人的技能让我的生活更轻松,当事情没有按预期的方式进行时,我会感到非常沮丧 让我们从我的输入开始: $j_source = '{"accountIndex":' . $wallet_idx . ',"walletId": "'. $wallet_id . '"}'; $j_destination = '{"address":"' . $banker . '","amount":' . $lovelace . '}
$j_source = '{"accountIndex":' . $wallet_idx . ',"walletId": "'. $wallet_id . '"}';
$j_destination = '{"address":"' . $banker . '","amount":' . $lovelace . '}';
$source = json_decode($j_source, true);
$destination = json_decode($j_destination, true);
我有两个变量是JSON的数组。这个函数希望输入为数组,所以这就是我想要给它的
有问题的函数():
运行此函数时,结果为错误:
阵列([状态]=>错误[诊断]=>阵列([验证错误]=>
分析类型的构造函数付款时,$中出错
Cardano.Wallet.API.V1.Types.Payment预期对象但获得数组。)
[消息]=>JSONValidationFailed)
所以,很明显,我试图将数组作为对象发送给它,但有一个错误,告诉我它想要的是对象而不是数组。所以现在我在兜圈子,我不知道下一步该怎么办
这是我使用的cURL POST函数:
// CURL Post
private function post(string $endPoint, $postFields = []): string
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->host.$endPoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_PORT, $this->port);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLCERT, Cardano::CERT_PATH);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$headers = [
"Cache-Control: no-cache",
"Content-Type: application/json;charset=utf-8",
"Postman-Token: b71b609e-e028-d78b-ce51-3d0ec0b5c9fb",
"accept: application/json;charset=utf-8"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if($this->disableSSLVerification)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
$data = curl_exec ($ch);
$this->httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if(curl_exec($ch) === false)
{
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close ($ch);
return $data;
}
据我所知,这个CURL代码是使用一种叫做“Postman”的东西自动生成的(我下载了它,但我不知道如何使用它,所以一点帮助都没有)
问题是,我不知道怎么了。我不知道CURL的构造方式是否有问题,我不知道它是否在包含createNewTransaction函数的类中有问题
我所知道的是,库中的GET函数可以正常工作。我可以运行GET查询并毫无问题地返回有效的JSON
如果你能给我一些关于什么地方出了问题的提示或线索,我也许可以自己找出答案,我只是不知道从哪里开始 这里有很多错误,$j_source和$j_destination的编码不正确(例如,如果$banker的名称中有一个“in-the-name”,会发生什么情况?json_解码将失败并返回NULL,任何试图读取$destination成员的操作都会导致php崩溃,出现异常),curl_setopt的任何和所有潜在错误都会被默默地忽略。但这里实际的immidate问题似乎是返回json_encode($postFields)时出现了问题在函数
post
中,你知道,我现在真的不担心细节,我主要关心的是如何让它工作。如果没有任何效果,那么细节就没有任何意义。我将感谢更多的帮助和更少的评论。谢谢……当你遇到错误时,json_encode($postFields)在post函数中返回什么?
// CURL Post
private function post(string $endPoint, $postFields = []): string
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->host.$endPoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_PORT, $this->port);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLCERT, Cardano::CERT_PATH);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$headers = [
"Cache-Control: no-cache",
"Content-Type: application/json;charset=utf-8",
"Postman-Token: b71b609e-e028-d78b-ce51-3d0ec0b5c9fb",
"accept: application/json;charset=utf-8"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if($this->disableSSLVerification)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
$data = curl_exec ($ch);
$this->httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if(curl_exec($ch) === false)
{
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close ($ch);
return $data;
}