首页 > 解决方案 > 我如何将正确类型的对象或数组发送到 curl 函数

问题描述

我对 cURL 的了解为零,这对我来说都是希腊语,而且我的 PHP 技能有限。我依靠别人的技能来让我的生活更轻松,当事情没有按照他们应该的方式工作时,我会感到非常沮丧。

让我们从我的输入开始:

 $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); 

我有 2 个 vars 是 JSON 数组。该函数希望输入为数组,所以这就是我试图给它的。

有问题的功能(最初来自这里):

// Generates a new transaction from the source to one or multiple target addresses.
    public function createNewTransaction(array $source, array $destination, string $spendingPassword): array
    {
        $groupPolicy = 'OptimizeForHighThroughput';
        return self::jsonDecode($this->post('/api/v1/transactions?source='.$source.'?destination='.$destination.'?groupingPolicy='.$groupPolicy.'?spendingPassword='.$spendingPassword), true);
    } 

当我运行这个函数时,结果是一个错误:

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

如果你能给我一些关于什么是错的提示或线索,我可能会自己弄清楚,我只是不知道从哪里开始。

标签: phparrayscurl

解决方案


推荐阅读