coingate / coingate-php

CoinGate PHP library for API v2
https://coingate.com
MIT License
51 stars 34 forks source link

encoded issue #8

Open thegbomb opened 6 years ago

thegbomb commented 6 years ago

I am passing an array: $postParams = array( 'order_id' => $cart_order_id, 'price_amount' => $grandTotal, 'price_currency' => 'XBT', 'receive_currency' => 'XBT', Etc Which seems to be passed into the curl_exec as: “order_id=180605-102147-3094&price_amount=0.002831367&price_currency=XBT&receive_currency=XBT&”

(http_build_query($params) produces the above string)

It looks like there is encoding is happening somewhere because when I look in https://coingate.com/account/apps/api-requests

I can see “{"order_id":"180605-102147-3094","amp;price_amount":"0.002831367","amp;price_currency":"XBT","amp;receive_currency":"XBT","amp;” etc

Obviously somewhere the & is encoded to & but then not decoded again.

Any clues to solving this?

thegbomb commented 6 years ago
@@ -65,13 +65,13 @@ class CoinGate
             CURLOPT_RETURNTRANSFER => 1,
             CURLOPT_URL            => $url
         );

         if ($method == 'POST') {
-            $headers[] = 'Content-Type: application/x-www-form-urlencoded';
+            $headers[] = 'Content-Type: application/json';
             array_merge($curl_options, array(CURLOPT_POST => 1));
-            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
+            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
         }

         curl_setopt_array($curl, $curl_options);
         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
         curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);

This seems to solve the issue

thegbomb commented 6 years ago

After some pondering, I suspect this could also work:

-            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
+            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params, '', '&'));