Created
August 28, 2016 23:59
-
-
Save jotapepinheiro/49400ae44fa9b59e0afee6703127e836 to your computer and use it in GitHub Desktop.
Formata Moeda PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$preco=Convert('USD', 'BRL','1,00'); | |
if($preco!=false){ | |
echo $preco; | |
} | |
else{echo "conversão não possível";} | |
function Convert($from, $to, $preco) | |
{ | |
$url = "http://www.google.com/finance/converter?a=$preco&from=$from&to=$to"; | |
$data = getPage($url); | |
if(empty($data)) | |
return false; | |
$dom = new DOMDocument(); | |
@$dom->loadHTML($data); | |
$return = @$dom->getElementById('currency_converter_result') | |
->getElementsByTagName('span') | |
->item(0) | |
->firstChild | |
->wholeText; | |
$return = (float)$return; | |
if($return == 0 ) | |
return false; | |
return number_format($return,2); | |
} | |
function getPage($url) | |
{ | |
if(ini_get('allow_url_fopen') != 1) { | |
@ini_set('allow_url_fopen', '1'); | |
} | |
if(ini_get('allow_url_fopen') != 1) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL,$url); | |
curl_setopt($ch, CURLOPT_FAILONERROR, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 3); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
} | |
else { | |
$data = file_get_contents($url); | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment