Created
November 23, 2019 00:11
-
-
Save dalibor-sojic/a61ee6bd36d6aca3558d2391a4d48f10 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 | |
namespace App\Helpers; | |
use Sunra\PhpSimple\HtmlDomParser; | |
class UJPCrawler | |
{ | |
static public function ujpInfo($companyName) | |
{ | |
$searchResultURL = "http://ujp.gov.mk/mk/prebaruvanje_pravni_lica/prebaruvaj?".http_build_query(["pravno_lice"=>$companyName]); | |
$html = HtmlDomParser::file_get_html($searchResultURL); | |
if ($html->find("table[class=tabela]",0) == null) { | |
$app = app(); | |
$erroObj = $app->make('stdClass'); | |
$erroObj->message = "Items not found. Please make more concise and correct searh"; | |
return json_encode($erroObj); | |
} | |
//Get all td from table with class tabela | |
$allTableData = $html->find("table[class=tabela]",0)->find("td"); | |
//If found only one item, get details for that company | |
if(count($allTableData) / 2 === 1){ | |
$app = app(); | |
$companyObj = $app->make('stdClass'); | |
$companyObj->details = $app->make('stdClass'); | |
$companyObj->details->скратенНазив = $allTableData[11 | |
]->find("a",0)->text(); | |
$companyDetailsURL = "http://ujp.gov.mk/mk/prebaruvanje_pravni_lica/prikazi?edb="; | |
$edb = explode("=",$allTableData[1]->find("a",0)->href)[1]; | |
//Get new html for company details with given edb | |
$html = HtmlDomParser::file_get_html($companyDetailsURL.$edb); | |
//Get all tr from table with class tabela, skip first and iterate | |
$allTableData = $html->find("table[class=tabela]",0)->find("tr"); | |
foreach (array_slice($allTableData,1) as $element){ | |
$key = trim($element->find("td",0)->plaintext); | |
$companyObj->details->$key = trim($element->find("td",1)->plaintext); | |
} | |
return json_encode($companyObj,256); | |
} | |
//If found more items in search return them with included edb | |
$companies = []; | |
$tmpArray = []; | |
foreach ($allTableData as $element){ | |
if($element->find("a",0) != null){ | |
$tmpArray['naziv'] = trim($element->plaintext); | |
$tmpArray['edb'] = explode("=",$element->find("a",0)->href)[1]; | |
array_push($companies, $tmpArray ); | |
$tmpArray = []; | |
} | |
} | |
$tmpArray = ['rezult' => "list", "firmi" => $companies]; | |
return json_encode($tmpArray,256); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment