0

I´m forking this repo https://github.com/FundacionPesetacoin/Pesetacoin_WooCommerce-Plugin and working fine. But when change the API for catch the price in other Site, not update

I try some differents links of API and make same. Original code get info of his private API, and I want use other public API.

With original code, API show this info:

{"status" : "success" , "message" : "null", "ptc_btc" : "0.00000083", "btc_usd" : "5070.29", "btc_eur" : "4505.46", "supply" : "138188628.56442260", "ptc_eur" : "0.00373953", "ptc_usd" : "0.00420834" , "date" : "2019-04-13 10:20:07"}

and get "ptc_eur" of API for shows in shoppping cart. Now I want use the new API of other site https://api.coingecko.com/api/v3/simple/price?ids=reecore&vs_currencies=eur than shows this info:

{"reecore":{"eur":0.0046564}}

I want use only the "eur" data , same the original code use the "ptc_eur" but dont work. Sorry for my english.

ORIGINAL CODE:

        //precio en PesetaCoins
global $woocommerce;
$euros= $woocommerce->cart->total;  
    $xaxa= "http://nodos.pesetacoin.info/api/api.php";
    $data = file_get_contents($xaxa);
$pesetas = json_decode($data, true);
    $valor_ptc= $pesetas['ptc_eur'];
        $ptc= $euros/$valor_ptc;
        $ptc= round($ptc, 2);
//precio en PesetaCoins

    $pagos= array();

        $metodo= $order->get_payment_method();

                        $i = -1;
                        foreach ( $this->account_details as $account ) {
                                $i++;
                            $pagos[$i]=     
                                $pagos[$i]= esc_attr( wp_unslash( $account['hash_name'] ) );
                        }

$cont= rand(0, $i);

        if($metodo == "ptc") {
        $description= "<span style='font-size:14px'>Para completar el pedido, debe enviar la cantidad <b>".$ptc."</b> de Pesetacoin a la siguiente dirección: <b>";
        $description.= $pagos[$cont];
        $description.="</b><br>Una vez se reciba la transacción se enviará el pedido.</span>";
        echo wpautop(wptexturize($description));


        }


    }

NEW CODE:

        //precio en ReecoreCoins
global $woocommerce;
$euros= $woocommerce->cart->total;  
    $xaxa= "https://api.coingecko.com/api/v3/simple/price?ids=reecore&vs_currencies=eur";
    $data = file_get_contents($xaxa);
$pesetas = json_decode($data, true);
    $valor_reex= $pesetas['eur'];
        $reex= $euros/$valor_reex;
        $reex= round($reex, 2);
//precio en ReecoreCoins

    $pagos= array();

        $metodo= $order->get_payment_method();

                        $i = -1;
                        foreach ( $this->account_details as $account ) {
                                $i++;
                            $pagos[$i]=     
                                $pagos[$i]= esc_attr( wp_unslash( $account['hash_name'] ) );
                        }

$cont= rand(0, $i);

        if($metodo == "reex") {
        $description= "<span style='font-size:14px'>Para completar el pedido, debe enviar la cantidad <b>".$reex."</b> de Reecorecoin a la siguiente dirección: <b>";
        $description.= $pagos[$cont];
        $description.="</b><br>Una vez se reciba la transacción se enviará el pedido.</span>";
        echo wpautop(wptexturize($description));


        }


    }
Linda Paiste
  • 38,446
  • 6
  • 64
  • 102
  • 1
    I'm sorry, but I have no idea what your actual question is. You need to edit the question and explain a bit better what it is you're asking/having issues with. It's currently very unclear. – M. Eriksson Apr 13 '19 at 09:30
  • Edited , sorry for not good explain.Is my first post. – Cryptoextrem Apr 13 '19 at 09:52

3 Answers3

1

It's because the now Coingecko API return a nested JSON which is simply a JSON file with a fairly big portion of its values being other JSON objects.

Compared with Simple JSON, Nested JSON provides higher clarity in that it decouples objects into different layers, making it easier to maintain.

Using Phrase, keys will be stored by separating levels with a dot.

0

The new API returns a nested JSON object, where you need two steps to access the desired value:

$valor_reex= $pesetas['reecore']['eur'];
hlg
  • 1,321
  • 13
  • 29
0

You might want to use ready library for this. Like this one https://github.com/npabisz/coingecko-api.

Install via composer:

composer require npabisz/coingecko-api

And then get your reecore price by:

$client = new \CoinGecko\Client();
        
$data = $client->Simple->Price->get([
    'ids' => 'reecore',
    'vs_currencies' => 'eur',
]);

$reecorePrice = $data['reecore']['eur'] ?? null;
Yorki
  • 21
  • 3