How to extract content from a PHP Page?

1

Personally I'm basically trying to get some data from SEFAZ, to check if it is online or unavailable at the moment I'm not getting it because I do not know, I wonder how I can get a certain line of their code?

    
asked by anonymous 16.07.2015 / 23:11

1 answer

4

You can use get_http_response_code to check that the page is online and then file_get_contents to download the page.

if(get_http_response_code('http://sefaz.com') != "200"){
    echo "erro";
}else{
    $pagina = file_get_contents('http://sefaz.com');
}

To find a certain line after downloading the page, in this case the choice of the AP is by the content of the MT line the library SIMPLE_HTML_DOM_PARSER

<?php
    include '../../../library/Simple_HTML_DOM/simple_html_dom.php';
    $html = file_get_html( 'http://www.nfe.fazenda.gov.br/portal/disponibilidade.aspx?versao=0.00&tipoConteudo=Skeuqr8PQBY=' );
    $tdMT = NULL;
    foreach($html->find('td') as $td){
        if($td->innertext === 'MT'){
            $tdMT = $td->parent();
            foreach($tdMT->find('td') as $td){
                echo $td->innertext . '<br>';
            }
        }
    }
?>

What's being done:

  • The inclusion of the Simple_HTML_DOM
  • Full page download
  • Search by tags td
  • A check is made if the td is the desired one (in the case MT )
  • Assignment of parent element of this td in case to tr
  • All elements present within td daughter of tr (any computation could be done)
  • * Note: There is also plain text such as MT and - HTML tags within td .

    Link to library download: Link

        
    16.07.2015 / 23:17