Skip to content
Advertisement

How to get current Bitcoin rate by dollars [closed]

I would like to get the current Bitcoin price on Backend PHP code I hope to use that as an global function on Laravel. Thanks.

Advertisement

Answer

Hope it’s would be helpful and let me know if there is any other issues.

if (!function_exists("getCurrentBtcDollar")) {
    function getCurrentBtcDollar() {
        //
        $url='https://bitpay.com/api/rates';
        $json=json_decode( file_get_contents( $url ) );
        $btc=0;

        foreach( $json as $obj ){
            if( $obj->code=='USD' )$btc=$obj->rate;
        }

        return $btc;
    }
}

Define that function on Laravel helper. You could create a new PHP file on “AppHttpsHelpersHelpers.php” And register that files on “composer.json” file on the root of the project.

"autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "files": ["app/Http/Helpers/helpers.php"]
    },

Insert that line onto there. That’s all. Regards

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement