Cotação do Dólar em PHP + XML
Publicado por Silvio Garbes Lara (última atualização em 19/08/2009)
[ Hits: 14.146 ]
Homepage: www.silviogarbes.com.br
Download 4127.cotacaoDolar.php
Script desenvolvido em PHP + XML que mostra a cotação do dólar direto do site do Banco Central do Brasil ( http://www.bcb.gov.br/ ) e exibe para o usuário em uma tabela formatada. Os campos mostrados são Câmbio (em R$)(dólar EUA) para compra e venda.
<?php
// -------------------------------- //
// Origem: http://sites.google.com/site/silviogarbes/dicas/cotacao-do-dolar
// Autor: Slvio Garbes Lara <silviogarbes@gmail.com>
// http://www.silviogarbes.com.br
// -------------------------------- //
function cotacaoDolar(){
//abrindo arquivo
if(!$fp=fopen("http://www4.bcb.gov.br/feed/taxas.ashx" ,"r" )) exit;
$conteudo = "";
//lendo arquivo
while(!feof($fp)) {
$conteudo .= fgets($fp,1024);
}
//fechando arquivo
fclose($fp);
$xml = new SimpleXMLElement($conteudo);
//echo "<pre>";
//print_r($xml);
for($i=0; $i<count($xml->channel->item); $i++){
if($xml->channel->item[$i]->guid == 'US$PTAX'){
$ptax_id = $i;
}else
if($xml->channel->item[$i]->guid == 'US$'){
$dolar_id = $i;
}
}
$texto_ptax_temp = explode("<div id='value'>",$xml->channel->item[$ptax_id]->description);
$texto_ptax_temp1 = explode("</div>",$texto_ptax_temp[1]);
$texto_ptax_temp2 = explode("</div>",$texto_ptax_temp[2]);
$texto_ptax['data'] = substr($xml->channel->item[$ptax_id]->pubDate,5,2)." ".substr($xml->channel->item[$ptax_id]->pubDate,8,3)." (PTAX)";
$texto_ptax['compra'] = $texto_ptax_temp1[0];
$texto_ptax['venda'] = $texto_ptax_temp2[0];
$texto_dolar_temp = explode("<div id='value'>",$xml->channel->item[$dolar_id]->description);
$texto_dolar_temp1 = explode("</div>",$texto_dolar_temp[1]);
$texto_dolar_temp2 = explode("</div>",$texto_dolar_temp[2]);
$texto_dolar['data'] = substr($xml->channel->item[$dolar_id]->pubDate,5,2)." ".substr($xml->channel->item[$dolar_id]->pubDate,8,3)."";
$texto_dolar['compra'] = $texto_dolar_temp1[0];
$texto_dolar['venda'] = $texto_dolar_temp2[0];
//echo "</pre>";
$tabela_cotacao = "<center>
<font face=\"Tahoma\" color=\"#000080\" style=\"font-size: 9pt\">Cmbio – Dlar EUA</font><br>";
$tabela_cotacao = $tabela_cotacao."
<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" style=\"border-collapse: collapse\">
<tr>
<th><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">Data</font></th>
<th><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">Compra</font></th>
<th><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">Venda</font></th>
</tr>
<tr align=\"center\">
<td><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">".$texto_ptax['data']."</font></td>
<td><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">".$texto_ptax['compra']."</font></td>
<td><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">".$texto_ptax['venda']."</font></td>
</tr>
<tr align=\"center\">
<td><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">".$texto_dolar['data']."</font></td>
<td><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">".$texto_dolar['compra']."</font></td>
<td><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">".$texto_dolar['venda']."</font></td>
</tr>
<tr>
<td colspan=3><font face=\"Tahoma\" color=\"#000000\" style=\"font-size: 7pt\">Fonte: <a href=\"http://www.bcb.gov.br\">www.bcb.gov.br</a></font></td>
</tr>
</table>
";
$tabela_cotacao = $tabela_cotacao."</center>";
return $tabela_cotacao;
}
echo cotacaoDolar();
?>
Cinnamon seria a aposta acertada frente às outras interfaces gráficas mais populares?
KDE Plasma - porque pode ser a melhor opção de interface gráfica
Gentoo: detectando impressoras de rede e como fixar uma impressora por IP
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Vou destruir sua infância:) (8)
Interface gráfica com problema (2)
Instalar Linux em notebook Sony Vaio VPCEG13EB (13)









