3
This code takes the purchase and sale values of the dollar and euro of the site.
I would also like to get the var(%) which is the 5th column.
See the source code and how it works by clicking here
<?php
if(!$fp=fopen("https://www.infomoney.com.br/mercados/cambio" , "r" ))
{
echo "Erro ao abrir a página de cotação" ;
exit;
}
$conteudo = '';
while(!feof($fp))
{
$conteudo .= fgets($fp,1024);
}
fclose($fp);
$valorCompraHTML = explode('class="numbers">', $conteudo);
$valorCompra = trim(strip_tags($valorCompraHTML[5]));
$valorVendaHTML = explode(' ', strip_tags($valorCompraHTML[6]));
//Estes são os valores HTML para exibir no site.
$valorVendaHTML = explode(' ', $valorVendaHTML[0]);
$valorVenda = trim($valorVendaHTML[0]) ;
//Compra Turismo.
$valorCompraT = trim(strip_tags($valorCompraHTML[7]));
$valorCompraT = explode(' ', $valorCompraT);
$valorCT = trim($valorCompraT [0]) ;
//Venda Turismo.
$valorVendaT = trim(strip_tags($valorCompraHTML[8]));
$valorVendaT = explode(' ', $valorVendaT);
$valorVT = trim($valorVendaT[0]) ;
//Compra Euro.
$valorCompraE = trim(strip_tags($valorCompraHTML[11]));
$valorCompraE = explode(' ', $valorCompraE);
$valorCE = trim($valorCompraE[0]) ;
//Venda Euro.
$valorVendaE = trim(strip_tags($valorCompraHTML[12]));
$valorVendaE = explode(' ', $valorVendaE);
$valorVE = trim($valorVendaE[0]) ;
//Estes são os valores numéricos para cálculos.
$valorCompraCalculavel = str_replace(',','.', $valorCompra);
$valorVendaCalculavel = str_replace(',','.', $valorVenda);
?>
<table class="table table-bordered table-hover">
<thead style="background: #ddd;">
<tr>
<td><b><center>Moeda</center></b></td>
<td><b><center>Compra</center></b></td>
<td><b><center>Venda</center></b></td>
</tr>
</thead>
<tbody>
<tr>
<td>Dólar Comercial</td>
<td>R$ <?php echo $valorCompra ?></td>
<td>R$ <?php echo $valorVenda ?></td>
</tr>
<tr>
<td>Dólar Turismo</td>
<td>R$ <?php echo $valorCT ?></td>
<td>R$ <?php echo $valorVT ?></td>
</tr>
<tr>
<td>Euro</td>
<td>R$ <?php echo $valorCE ?></td>
<td>R$ <?php echo $valorVE ?></td>
</tr>
</tbody>
</table>
You can put the link code in the question?
– rray
As well var(%) which is the 5th column?
– viana
Accessing the website of Infomoney you will see a table. The column Var(%) is the one that shows the percentage of variation between the previous and the current value.
– convidado
A tip for everyone
DOMXPath::query
, if no one is willing to formulate a response with this, as soon as possible I will provide an example.– Guilherme Nascimento