Ejemplo n.º 1
0
 public function indexAction()
 {
     $request = $this->getRequest();
     if ($request->isPost()) {
         $scraping = new Scraping();
         $cnpj = $request->getPost()->num_cnpj;
         $render = $scraping->spider($cnpj);
         return array("flash" => $render);
     }
     return new ViewModel();
 }
Ejemplo n.º 2
0
 public function spider($cnpj = null)
 {
     $arr_final = [];
     if ($this->validate_cnpj($cnpj) == false) {
         return ['error' => true, 'message' => 'Numero de CNPJ incorreto.'];
     }
     try {
         $response = $this->fetch($cnpj);
         $dom = new Query($response);
         $results = $dom->execute('table.resultado');
         // #seed1
         $secao = $dom->execute('td.secao');
         // #seed2
         $tables = $dom->execute('table.resultado table');
         // #seed3
         if (count($results) == 1 && count($secao) == 3) {
             $titulos = $dom->execute('td.titulo');
             $valores = $dom->execute('td.valor');
             $scraping = new Scraping();
             $arr_tt = $scraping->getSectionHtml($titulos, ['formatter' => 'l', 'special' => true, 'sanitize' => true]);
             $arr_vl = $scraping->getSectionHtml($valores, ['formatter' => 'l', 'special' => false, 'sanitize' => false]);
             if (count($arr_vl) == 18) {
                 unset($arr_vl[count($arr_vl) - 1]);
                 // #fix
             }
             if (count($arr_tt) == count($arr_vl)) {
                 $arr_final = array_combine($arr_tt, $arr_vl);
                 return ['error' => false, 'message' => 'Dados recuperados com sucesso', 'json' => $arr_final];
             }
         } else {
             return ['error' => true, 'message' => $cnpj . ' não existente em nossa base de dados'];
         }
     } catch (\Exception $e) {
         return $e->getMessage();
     }
     return $arr_final;
 }
 public function testSpiderKeyExistsError()
 {
     $scraping = new Scraping();
     $arr_final = $scraping->spider();
     $this->assertEquals($arr_final['error'], true);
 }