Site icon Shaharia's Blog

Get Bangladesh Stock Market (DSE, CSE) Price in PHP

Since from the beginning of my career as a professional PHP programmer, I fell in love with web scraping. And I have built so many tools (may be more than 20) that collect data and make it re-usable easily. And suddenly I got an idea to build a PHP script to get DSE share price in PHP.

So here it comes. I built it. It was an easy task but very useful for developer to display and use DSE, CSE share price in real-time from their own PHP application.

So anybody can use that public data without going to DSE and CSE official website. But it depends how you use it and where you use it.

Why it’s a very useful PHP library?

Imagine, you have a website or blog or an application. And you want to provide real-time share price data of Dhaka stock exchange and Chittagong stock exchange. What you need to do? You need to get the data from their website. Because Bangladeshi government bodies or there is no third party who can provide you data that you can use in your application programmatically.

This types of data we can get easily for foreign stock market and other data sources. But not for Bangladeshi stock market.

That’s why I wrote this library. This PHP library simply scrap the website of Dhaka stock exchange and Chittagong stock exchange and provide you a dataset that you can transform in your own way that is comfortable for you.

Installation

Installing this PHP library is very easy to get DSE share price and CSE share price. You can easily install this with composer in your project. Just run the following command to add this library as a dependency in your project.

composer require shahariaazam/bd-stock-exchange

This will do most of the hard job. Thanks to composer. Now keep reading to understand how to get the share price of DSE and CSE from this PHP library.

Get Dhaka Stock Exchange (DSE) Share Price

To get the share price of Dhaka Stock Exchange just copy/paste the following lines of code. Even any novice PHP programmer can also implement this small piece of codes and can be understood easily.

<?php

use ShahariaAzam\BDStockExchange\StockExchange\ChittagongStockExchange;
use ShahariaAzam\BDStockExchange\StockExchange\DhakaStockExchange;
use ShahariaAzam\BDStockExchange\StockPrice;

require "vendor/autoload.php";

$dse = new DhakaStockExchange();    // For Dhaka Stock Exchange
// $cse = new ChittagongStockExchange();    // For Chittagong Stock Exchange

$stock = new StockPrice();
$stock->setStockExchange($dse);
var_dump($stock->getPricing());     // Return PricingEntity[]
var_dump($stock->toArray());        // Return as array

Output of getDSEPricing( ) method


Array
(
    [0] => Array
        (
            [company] => 1JANATAMF
            [lastTradeValue] => 6.5
            [changeInAmount] => 0.2
            [changeInPercentage] => 3.17
            [highPrice] => 
            [lowPrice] => 
        )

    [1] => Array
        (
            [company] => 1STPRIMFMF
            [lastTradeValue] => 21.6
            [changeInAmount] => 1.5
            [changeInPercentage] => 7.46
            [highPrice] => 
            [lowPrice] => 
        )

Read more articles on PHP