Search code examples
laravelbinance

Binance API call not working in Lavavel Controller


I am writing a laravel controller which calls Binance PHP APIs.

The PHP API works perfect if run individually from command line, e.g., php price.php

+++++++price.php++++++++

$api = new \Binance\API($api_key, $api_secret);

// Get all of your positions, including estimated BTC value $price =$api->price("BNBBTC"); print_r($price);

+++++++price.php+++++++++

However, if I call the api funcion price() from laravel controller, nothing shows up, no errors etc. I can dd($binance_api) and it returned the object is created successfully with all the correct API key/secret.

Class PriceController extends Controller{
public function price (Request $request){

$api_key = "xxxxxxx";

$api_secret = "xxxxxxxx";

$binance_api = new \Binance\API($api_key, $api_secret);

$price = $binance_api->price("BNBBTC");

}

}


Solution

  • You need to return a value

    Class PriceController extends Controller{
      public function price (Request $request){
        $api_key = "xxxxxxx";
        $api_secret = "xxxxxxxx";
        $binance_api = new \Binance\API($api_key, $api_secret);
        $price = $binance_api->price("BNBBTC");
        return $price;
      }
    }