How to return a part of a string (an html element) in php?

Asked

Viewed 516 times

3

I am making a "technical resource" to be able to return the status of the SEFAZ servers of issuing tax notes using PHP. For this, I have until now the code below:

function get_content($URL){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $URL);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$d = get_content('http://www.nfe.fazenda.gov.br/portal/disponibilidade.aspx');
echo $d;

This code returns me the page that the recipe provides without need of digital certificate to see the status of the servers.

I need to know how I can get only an existing HTML element inside the variable $d (the element is the table.tabelaListagemDados) and from his values I can do treatments.

I know I can handle this information using javascript (pure, with Angularjs or with jQuery) but if there is any method through PHP it is better because then I can return only one JSON and not a whole page.

Someone sees a solution on how I can do to only get this element inside the page?

  • 3

    These links should help: http://answall.com/questions/44694/capturr-e-filtrar-result, http://answall.com/questions/78621/comoros-extrair-data

  • For sure @bfavaretto! They helped me a lot! gave work but ta there the library (if you need the link ta in the question)

2 answers

3

See if that helps you:

$data = get_content('http://www.nfe.fazenda.gov.br/portal/disponibilidade.aspx');
$dom = new domDocument;
@$dom->loadHTML($data);
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item(1)->getElementsByTagName('tr');

foreach ($rows as $row) {
        $cols = $row->getElementsByTagName('td');
        print_r($cols);
}
  • 1

    Tks @Samir Braga

  • This helps me yes but I can not only take the elements tr inside the table... there in the If they have put a tag caption that contains the time of the query result, this is interesting too

  • once you have everything straight, returning as JSON I put on the stack wiki

1

Come on, guys, I got my problem solved. Below is the script that takes the page that shows the availability of the electronic invoice sending webservices and transforms the content into a json.

https://github.com/leandroluk/wsDisponibilidadeSefaz/

For those who want to use, feel free, but please leave the credits.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.