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

require "vendor/autoload.php";

$dse = new \ShahariaAzam\BDStockExchange\StockPrice();
var_dump($dse->getDSEPricing());

If I explain the above code you will see that there is a \ShahariaAzam\BDStockExchange\StockPrice class. And if you open that class, you will see there are two methods to get the Bangladeshi stock market price. One is getDSEPricing() and another one is getCSEPricing().

You can understand which method for which purpose based on method naming.

Output of getDSEPricing( ) method

array(339) {
   [0] =>
   array(4) {
     'company' =>
     string(9) "1JANATAMF"
     'lastTrade' =>
     string(4) "6.10"
     'changeAmount' =>
     string(5) "-0.10"
     'changePercent' =>
     string(6) "-1.61%"
   }
   [1] =>
   array(4) {
     'company' =>
     string(10) "1STPRIMFMF"
     'lastTrade' =>
     string(5) "12.10"
     'changeAmount' =>
     string(4) "0.20"
     'changePercent' =>
     string(5) "1.68%"
   }
   [2] =>
   array(4) {
     'company' =>
     string(8) "AAMRANET"
     'lastTrade' =>
     string(5) "75.10"
     'changeAmount' =>
     string(5) "-2.50"
     'changePercent' =>
     string(6) "-3.22%"
   }

I have already developed a tiny tools in my website to see DSE share price in real-time. You can also check real-time Dhaka Stock Exchange share market price this tools that was built originally with this library. Also you can get JSON format of the DSE share market price in real-time. Get the DSE share price API in JSON from here.

Get Chittagong Stock Exchange (CSE) Share Price

If you understand the above code, it’s pretty same. At least 99% same except few more or less array key. Because DSE and CSE website display different types of information. So both dataset can’t display similar array keys for every data. Let’s see what it does.

Similar to the previous one which you used to get DSE share price. See the following code to get the CSE share price.

<?php

require "vendor/autoload.php";

$dse = new \ShahariaAzam\BDStockExchange\StockPrice();
var_dump($dse->getCSEPricing());

This will fetch the current stock price of Chittagong Stock Exchange as array that you can use however you want.

Output of getCSEPricing( ) method

See the following output.

array(339) {
   [0] =>
   array(4) {
     'company' =>
     string(9) "1JANATAMF"
     'lastTrade' =>
     string(4) "6.10"
     'high_price' =>
     string(4) "6.10"
     'low_price' =>
     string(4) "6.10"
     'yesterday_closing' =>
     string(5) "5.60"
     'changeAmount' =>
     string(6) "0.4%"
   }

If you see the output of getDSEPricing() and getCSEPricing() you will see what types of information are available by this library. It will help you to make plan on how to consume this data effectively.

Similar to the DSE Share market price, I have built another similar tools to get the share price of Chittagong Stock Exchange in real-time. You can check that tools by yourself. Also you can get CSE Share Price API in JSON from here.

100% open source PHP library

Most of my fun works are open source. I love to release my lovely works as open source so other can easily use those libraries and codes to save their time. I don’t know how many millions of hours I saved for others. Just kidding. 🙂

This PHP library to get DSE and CSE share price has been available on GitHub. If you face any issues implementing this on your project, please feel free to write a comments here or create an issue on GitHub from here.

It would be appreciated if you share this useful article and library with your friends. Happy coding and keep reading other PHP related articles.

Read more articles on PHP

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.