Search code examples
laravelapibinance

How can run Binance api in laravel with Controller


I can't use binance api in laravel

I installed Php binance api from composer require "jaggedsoft/php-binance-api @dev" but examples not working in laravel.I had some errors when I tried.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
require '../vendor/autoload.php';
class BinanceController extends Controller
{

public function list()
{
    $api = new Binance\API();
    $key = "----";
    $secret = "---";
    $api = new Binance\API($key, $secret);

    $price = $api->price("BNBBTC");
    return $price;

}

}

When I runned the route I got this error:

Symfony\Component\Debug\Exception\FatalThrowableError Class 'App\Http\Controllers\Binance\API' not found


Solution

  • You're not importing Binance\API correctly. Laravel believes the Binance\Api class is located in the App\Http\Controllers\Binance namespace. Which it is not.

    Try $api = new \Binance\API();

    Or put it in your use cases.

    use Binance\API

    I also found an old wrapper which you may be able to import if there hasn't been any changes to Binance since but I highly doubt it. Since your case is specific to Laravel, look for a Binance wrapper for Laravel specifically. Here may contain some useful information on how to use non-laravel packages, with laravel