Why json_encode() and http_build_query() does not work when curl request Qiwi API?

$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),
 THIS => 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);


You get the error:
"message" => """
 Invalid Json: Unrecognized token 'id': was expecting ('true', 'false' or 'null')\n
 at [Source: akka.util.ByteIterator$ByteArrayIterator$$anon$1@305cbddb; line: 1, column: 4]
 """


While if you replace :
http_build_query($request)
// at this
 $str = '{
"id":"11111111111111",
 "sum": {
"amount":100,
"currency":"643"
},
 "paymentMethod": {
"type":"Account",
"accountId":"643"
},
"comment":"test",
 "fields": {
"account":"+79121112233"
}
 }';

It turns out that putting the data into a string using "account":"+ . $phoneNum . "somehow this is not right, I think.

Beat error:
"code" => "QWPRC-220"
 "message" => "Insufficient funds "
]


Why doesn't json_encode() or http_build_query()? And the second question, how to enable test mode?)
June 14th 19 at 19:08
1 answer
June 14th 19 at 19:10
Solution
http_build_query() returns a url-encoded string and not a json response is appropriate. In the second example the json is going to "manually" turns what was expected, but instead, you can use json_encode().
Written in fact that json_encode() gives an error when you try to send a request to a url. The code I have is the same as in example - jaycee commented on June 14th 19 at 19:13
json_encode() was mentioned, but in the code, it is not, as the text of the corresponding error. - Jimmy.Pfeff commented on June 14th 19 at 19:16
Yes , you are right the code I posted with json encode, I wrote only the title that this feature just does not work.

CURLOPT_POSTFIELDS => json_encode($request),

Will give an even more terrible error that the query cannot be built. I refused until the QIWI payment and looked in the direction of other systems and APIs. Has proven to work with QIWI, it is necessary to go to Moscow and register, and still, they don't have the test mode. That is, you need to chase the money here and there for tests and pay them more for it, for what you are testing how it works for them. - jaycee commented on June 14th 19 at 19:19
Hmm, they were in test mode, just as managers are not always aware of. CURLOPT_POSTFIELDS can be either a url-encoded string or an array, either CURLFile. In the case of $str is probably the name of a variable was substituted as the key. Most likely "CURLOPT_POSTFIELDS => ['str'=>json_encode($request)]" would work, but I will not vouch. - Jimmy.Pfeff commented on June 14th 19 at 19:22

Find more questions by tags QiwiPHP