skip to Main Content

I created a wordpress plugin to retrieve the information of a token on pancakeswap. I don’t understand why it doesn’t show me the data. if someone can look at my code and tell me what’s wrong that will be super nice. thank you in advance.

<?php
/* 
Plugin name: WP PancakeSwap
Description: Ce plugin nous permet de dialoguer avec l' api PancakeSwap
Author: Jean Philippe Faucon
Version : 1.0
*/


/*  Copyright 2021 Jean Philippe Faucon  (email : [email protected])

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as 
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// Utilisation de l'API Pancakeswap.
// https://github.com/pancakeswap/pancake-info-api/blob/develop/v2-documentation.md
// Source : https://api.pancakeswap.info/api/v2/tokens/0xdb72feadd4a0734d62fa5a078551986519dca19d

// 1 étape : récupérer les infos auprès de PancakeSwap

function _get_wp_pancakeswap_datas () {
    
    $args = array (
        'timeout' => 120,
        'httpversion' => '1.1'
);
    $url = "https://api.pancakeswap.info/api/v2/tokens/0xdb72feadd4a0734d62fa5a078551986519dca19d";
    $call = wp_remote_get($url, $args);

    $response  =  wp_remote_retrieve_body($call);

    return $response;

}

// 2 étape : mettre en forme les données
add_shortcode('pancakeswap','output_pancakeswap');

function output_pancakeswap() {
    $datas = _get_wp_pancakeswap_datas () ;

    //Nom et prix du token

    $output = 'Nom du token : '.$datas->name;
    $output .= '<br>';
    $output .= 'Valeur du token : '.$datas->price;
    $output .= ' $';
    $output .= '<br>';
    $output .= 'Valeur du token en BNB : '.$datas->price_BNB;  
    return $output; 

}

2

Answers


  1. First of all you have to json decode the response.

    If this function return the body json decoded as the name suggest wp_remote_retrieve_body($call);

    try to "return $response->data" in the function _get_wp_pancakeswap_datas ()

    {"updated_at":1636744974029,"data":{"name":"Alfcoin","symbol":"ALF","price":"0.1937757238779150782534763119032","price_BNB":"0.000314980409577114948657924847012"}}

    If not you have to do something like this in the function _get_wp_pancakeswap_datas()

    $response = json_decode(wp_remote_retrieve_body($call));
    
    return $response->data;
    
    Login or Signup to reply.
  2. This is essentially right out of the WordPress manual.

    You need to convert the json response to a php object.
    You need to access the embedded object from the json response

    Alternatively, you could opt for the json_decode() 2nd parameter that returns a php array instead of a php object.

    function _get_wp_pancakeswap_datas () {  
      $args = array (
          'timeout' => 120,
          'httpversion' => '1.1'
      );
      $url = "https://api.pancakeswap.info/api/v2/tokens/0xdb72feadd4a0734d62fa5a078551986519dca19d";
      $call = wp_remote_get($url, $args);
      $response = json_decode(wp_remote_retrieve_body($call));
      return $response;
    }
    
    add_shortcode('pancakeswap','output_pancakeswap');
    
    function output_pancakeswap() {
      $response = _get_wp_pancakeswap_datas();
      //Nom et prix du token
      $datas = $response->data;
      $updateAt = $response->updated_at;
    
      $output = 'Nom du token : '.$datas->name;
      $output .= '<br>';
      $output .= 'Valeur du token : '.$datas->price;
      $output .= ' $';
      $output .= '<br>';
      $output .= 'Valeur du token en BNB : '.$datas->price_BNB;  
      return $output; 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search