I want to get the JSON dollar value displayed at this URL:
To use in my php file, can you do this with PHP only?
It also has this XML option:
With:
file_get_contents('http://api.promasters.net.br/cotacao/v1/valores?moedas=USD&alt=json');
It does not work, because this url is blocking by user_agent
but we can spoof it. Try this:
function get_page($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
$return = curl_exec($curl);
curl_close($curl);
return $return;
}
// true como segundo parametro do json_decode, signica que queremos os que vá buscar os conteudos como array em vez de ser como objeto, retire o true se quiser ir busca-los como objeto
$contents = json_decode(get_page('http://api.promasters.net.br/cotacao/v1/valores?moedas=USD&alt=json'), true);
print_r($contents); // Array ( [status] => 1 [valores] => Array ( [USD] => Array ( [nome] => Dólar [valor] => 3.5969 [ultima_consulta] => 1464713701 [fonte] => UOL Economia - http://economia.uol.com.br/ ) ) )
use this function
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = 0 [, int $maxlen ]]]] )
<?php
$json_url = "http://api.testmagazine.com/test.php?type=menu";
$json = file_get_contents($json_url);
$json=str_replace('},
]',"}
]",$json);
$data = json_decode($json);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
Another example:
$url = "http://link/data.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;exit;