Created
December 6, 2016 09:21
-
-
Save puncoz/d06b1c1662e2920a6e8249e997e17d4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class To Web-scrap nepse contents from MeroLagani Sites | |
*/ | |
class Nepse | |
{ | |
private $url; | |
private $curlResponse; | |
private $domDocument; | |
private $nepseData = array(); | |
private $nepseColName = array( | |
'stock_code', 'ltp', 'ltv', 'change_in_percent', 'high', 'low', 'open', 'qty' | |
); | |
function __construct($url) | |
{ | |
$this->url = $url; | |
$this->init(); | |
} | |
private function init() | |
{ | |
$ch = curl_init($this->url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$this->curlResponse = curl_exec($ch); | |
curl_close($ch); | |
$this->getDomDocument(); | |
$this->fetchNepseTable(); | |
} | |
private function getDomDocument() | |
{ | |
$this->domDocument = new DOMDocument(); | |
@$this->domDocument->loadHTML($this->curlResponse); | |
} | |
private function fetchNepseTable() | |
{ | |
$nepseTable = $this->domDocument->getElementById('ctl00_ContentPlaceHolder1_LiveTrading')->getElementsByTagName('tr'); | |
$this->nepseData = array(); | |
foreach ($nepseTable as $index => $row) { | |
if ($index == 0) continue; | |
$columns = $row->childNodes; | |
$colData = array(); | |
foreach ($columns as $cindex => $column) { | |
if (!isset($this->nepseColName[$cindex])) continue; | |
$colData[$this->nepseColName[$cindex]] = $column->textContent; | |
} | |
$this->nepseData[] = $colData; | |
} | |
} | |
private function getElementByClassName($className) | |
{ | |
$xpath = new DOMXPath($this->domDocument); | |
return $xpath->query("//*[@class='$className']"); | |
} | |
public function getAsOfDate() | |
{ | |
// $asofDate = $this->getElementByClassName('date-label'); | |
$asofDate = $this->domDocument->getElementById('live-trading-label-1'); | |
return $asofDate->textContent; | |
} | |
public function showHtml() | |
{ | |
return $this->domDocument->saveHtml(); | |
} | |
public function getNepseData() | |
{ | |
return $this->nepseData; | |
} | |
} | |
$meroNepse = new Nepse('http://merolagani.com/LatestMarket.aspx'); | |
echo '<pre>'; | |
print_r($meroNepse->getNepseData()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment