Get attribute value PHP DOM [closed]

1

I'm trying to make an application to grab certain values from a page and insert them into a page of mine. I do not know how to program, but I'm still risking it, because it's to improve my job (bid analyst). So far I have managed to get one of the data, but the other two I can not get. I'm using CURL to get session data, and Dom to get the values I need. Here's my code so far:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);


// iniciar sessao
$ch = curl_init();
// opcoes
curl_setopt($ch, CURLOPT_URL, "https://www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0');
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, ');

//executar sessao
$output = curl_exec($ch);
    if ($output === FALSE) {               // verifica erros no curl
       echo "cURL Error: " . curl_error($ch);
    }
//fechar sessao
curl_close($ch);

$dom = new DOMDocument;
$dom->loadHTML($output);
// Consultando atributo onclick
$codsprg = $dom->getElementsByTagName('a');
foreach ($codsprg as $codprg) {
$onclick[] = $codprg->getAttribute('onclick');
}
$origens  = array('Motivo_Suspensao(', ');', 'Dados_Pregao(');
$destinos = array('', '', '');
$nova = str_replace($origens, $destinos, $onclick);
$nova = array_filter($nova);
$nova = array_unique($nova);
$nova = array_slice($nova, 0);

// resultado
echo '<pre>';
//print_r ($nova);  Até aqui funciona

$numsprg = $dom->getElementsByTagName('a');
foreach ($numsprg as $numprg) {
$num[] = $numprg->getAttribute('onclick');
}
$new = $num;
echo $new;
?>

Code that I want to get: I need to get the data of the trading floor, which is in parentheses (I already have), the trading floor number that is on the same line as the trading floor data (ex: 512016) and uasg is on the bottom line (ex: 155023).

In the first part of the code I make the connection to CURL , then I use DOM to get the first one I need (data_prepare). After that I have tested several possible combinations with the DOM and always return errors like:

Trying to get property of non-object e 
Call to undefined method DOMDocument::find() in

I have already researched here in SOpt and tested all possible solutions, without success. The current error is:

Notice:  Array to string conversion in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\aclicita\teste.php on line 49

Line 49 is:

echo $new;
    
asked by anonymous 28.01.2017 / 23:20

1 answer

0

Well, after a while taking a look at the gift and its functions, I was able to get my code working. Below is code that is working.

<?php
header("Content-type: text/html; charset=utf-8");
error_reporting( error_reporting() & ~E_NOTICE );
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

//Converte caracteres acentuados para não acentuado
$conversao = array('á' => 'a','à' => 'a','ã' => 'a','â' => 'a', 'é' => 'e', 'ê' => 'e', 'í' => 'i', 'ï'=>'i', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', "ö"=>"o", 'ú' => 'u', 'ü' => 'u', 'ç' => 'c', 'ñ'=>'n', 'Á' => 'A', 'À' => 'A', 'Ã' => 'A', 'Â' => 'A', 'É' => 'E', 'Ê' => 'E', 'Í' => 'I', 'Ï'=>'I', "Ö"=>"O", 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ú' => 'U', 'Ü' => 'U', 'Ç' =>'C', 'Ñ'=>'N');

// iniciar sessao
$ch = curl_init();
// opcoes
curl_setopt($ch, CURLOPT_URL, "https://www.comprasnet.gov.br/pregao/fornec/Acompanhar.asp");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0');
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, 'ASPSESSIONIDSCRRTCSB=LPDMGOACKEJMOBEAJNBNELNC; ASPSESSIONIDQCQRRBQC=ENJMGOACPDMCKBMILFDFHMNJ');

//executar sessao
$output = curl_exec($ch);
    if ($output === FALSE) {               // verifica erros no curl
       echo "cURL Error: " . curl_error($ch);
    }
$output = strtr($output, $conversao); // função de conversão acentuado/não acentuado
//fechar sessao
curl_close($ch);

$dom = new DOMDocument;
$dom->loadHTML($output);
// Pega os códigos de cada pregão
$codsprg = $dom->getElementsByTagName('a');
foreach ($codsprg as $codprg) {
$onclick[] = $codprg->getAttribute('onclick');
}
$origens  = array('Motivo_Suspensao(', ');', 'Dados_Pregao(');
$destinos = array('', '', '');
$nova = str_replace($origens, $destinos, $onclick);  // remove palavras indejadas
$nova = array_filter($nova);
$nova = array_unique($nova);
$nova = array_slice($nova, 0);

// resultado
//echo '<pre>';
//print_r ($nova);

// Pegar os valores do número do pregão
$numsprg = $dom->getElementsByTagName('a');
foreach ($numsprg as $item) {
$prg[] = $item->nodeValue . "\n";
$prg = array_map('trim', $prg); // remove os espaços em branco
}
$origensprg  = array('Suspensao', 'Administrativa', 'Acompanhar', ' ');
$destinosprg = array('', '', '', '');
$newprg = str_replace($origensprg, $destinosprg, $prg); // remove palavras indejadas
$newprg = array_filter($newprg);
$newprg = array_slice($newprg, 0);
// print_r ($newprg);

//Pega os valores da uasg
$numsuasg = $dom->getElementsByTagName('td');
foreach ($numsuasg as $numuasg) {
$uasg[] = $numuasg->nodeValue . "\n";
$uasg = array_map('trim', $uasg); // remove os espaços em branco
}
$newuasg = preg_replace("/[^0-9]/", "", $uasg);   // remove caracteres não numericos do output
$newuasg = array_filter($newuasg);
$newuasg = array_slice($newuasg, 2);
//print_r($newuasg);
?>
    
04.02.2017 / 11:28