Example #1
0
 /**
  * Método para buscar as marcas existentes de acordo com o tipo informado
  *
  * @param  string $tipo
  * @param  int $tabelaReferencia
  * @throws Exception
  * @return array
  */
 public static function getMarcas($tipo = null, $tabelaReferencia = null)
 {
     if (is_null(FipeGratis::$__MARCAS) || is_null(FipeGratis::$__tipo) || FipeGratis::$__tipo != $tipo || is_null(FipeGratis::$__tabelaReferencia) || FipeGratis::$__tabelaReferencia != $tabelaReferencia) {
         FipeGratis::$__tabelaReferencia = $tabelaReferencia;
         FipeGratis::$__tipo = $tipo;
         FipeGratis::setParams($tipo);
         /*
          * Verificando se exite $tabelaReferencia
          */
         $validate = array_filter(FipeGratis::getTabelas(), function ($tables) use($tabelaReferencia) {
             return $tables['codigo'] == $tabelaReferencia;
         });
         if (empty($validate)) {
             throw new Exception("A tabela de referencia {$tabelaReferencia} não existe");
         }
         /*
          * Montando parametros
          */
         $paramters = array('ScriptManager1' => 'UdtMarca|ddlMarca', '__ASYNCPOST' => true, '__EVENTTARGET' => 'ddlMarca', '__LASTFOCUS' => '', '__EVENTARGUMENT' => FipeGratis::$__PARAM_EVENTARGUMENT, '__VIEWSTATE' => FipeGratis::$__PARAM_VIEWSTATE, '__VIEWSTATEGENERATOR' => FipeGratis::$__PARAM_VIEWSTATEGENERATOR, '__EVENTVALIDATION' => FipeGratis::$__PARAM_EVENTVALIDATION, 'ddlAnoValor' => 0, 'ddlMarca' => 0, 'ddlModelo' => 0, 'ddlTabelaReferencia' => $tabelaReferencia, 'txtCodFipe' => '');
         /*
          * Consultando modelos
          */
         $ch = curl_init("http://www.fipe.org.br/web/indices/veiculos/default.aspx?{$tipo}");
         $options = array(CURLOPT_COOKIEJAR => 'cookiejar', CURLOPT_HTTPHEADER => array("User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3", "Accept-Encoding: gzip, deflate", "Referer: http://www.fipe.org.br/web/indices/veiculos/default.aspx?{$tipo}", "Cookie: " . FipeGratis::$__COOKIE . "", "Host: www.fipe.org.br", "Connection: keep-alive", "X-MicrosoftAjax: Delta=true"), CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => http_build_query($paramters), CURLOPT_FOLLOWLOCATION => 1);
         curl_setopt_array($ch, $options);
         $html = curl_exec($ch);
         curl_close($ch);
         $explode = explode('|', $html);
         $key = array_search('__EVENTARGUMENT', $explode);
         FipeGratis::$__PARAM_EVENTARGUMENT = $explode[$key + 1];
         $key = array_search('__VIEWSTATE', $explode);
         FipeGratis::$__PARAM_VIEWSTATE = $explode[$key + 1];
         $key = array_search('__VIEWSTATEGENERATOR', $explode);
         FipeGratis::$__PARAM_VIEWSTATEGENERATOR = $explode[$key + 1];
         $key = array_search('__EVENTVALIDATION', $explode);
         FipeGratis::$__PARAM_EVENTVALIDATION = $explode[$key + 1];
         $crawler = new Crawler($html);
         $options = $crawler->filter('#ddlMarca > option');
         FipeGratis::$__MARCAS = array();
         $client = new Client();
         foreach ($options as $node) {
             if ($node->getAttribute('value') != '0') {
                 $logo = null;
                 //Tentando buscar logo
                 try {
                     $crawler = $client->request('GET', "https://www.google.com.br/search?tbm=isch&q={$node->nodeValue} logo");
                     $logo = $crawler->filter('#ires > table > tr:nth-child(1) > td:nth-child(1) > a >img')->attr('src');
                 } catch (Exception $e) {
                 }
                 FipeGratis::$__MARCAS[] = array('codigo' => $node->getAttribute('value'), 'marca' => $node->nodeValue, 'logo' => $logo);
             }
         }
     }
     return FipeGratis::$__MARCAS;
 }