B
B
BonBon Slick2017-10-07 14:53:31
PHP
BonBon Slick, 2017-10-07 14:53:31

Why json_encode() and http_build_query() don't work on Qiwi API curl request?

$headers = array(
            "Authorization: Bearer " . config('services.qiwi.key'),
            "Accept: application/json",
            "Content-Type: application/json"
        );
  $transactionId = '111111111111';
        $amount = 10.55;
        $currency = 643;
        $sum = ['amount' => $amount, 'currency' => $currency];
        $type = 'Account';
        $accountId = 643;
        $paymentMethod = ['type' => $type, 'accountId' => $accountId];
        $comment = 'test comment';
        $acc = "+22222"; // receive payments to
        $fields = ['account' => $acc];

        $request = [
            'id' => $transactionId,
            'sum' => $sum,
            'paymentMethod' => $paymentMethod,
            'fields' => $fields,
            'comment' => $comment,
        ];
  $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_POSTFIELDS => http_build_query($request),
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_SSL_VERIFYHOST => 2,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 60, // seconds
            CURLOPT_ENCODING => 'UTF-8'
        ));
        $result = curl_exec($curl);
        curl_close($curl);
        return json_decode($result, true);

Throws an error:
"message" => """
    Invalid Json: Unrecognized token 'id': was expecting ('true', 'false' or 'null')\n
     at [Source: [email protected]; line: 1, column: 4]
    """

Whereas if you replace :
http_build_query($request)
// на такое
        $str = '{
        "id":"11111111111111",
        "sum": {
          "amount":100,
          "currency":"643"
        },
        "paymentMethod": {
          "type":"Account",
          "accountId":"643"
        },
        "comment":"test",
        "fields": {
          "account":"+79121112233"
        }
      }';

It turns out that we sculpt the data into a string through "account":"+ . $phoneNum . ", somehow this is not correct, I think.
Throws an error :
"code" => "QWPRC-220"
  "message" => "Недостаточно средств "
]

Why doesn't json_encode() or http_build_query() work? And the second question, how to enable the test mode here?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vyrkmod, 2017-10-07
@BonBonSlick

http_build_query() returns a url-encoded string and not json, the answer is appropriate. In the second example, json is collected "manually", it turns out as expected, but json_encode() can be used instead.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question