How do I get JSON from a URL to use in my PHP file?

Asked

Viewed 3,681 times

2

  • I wanted to only take this dollar amount without using anything other than php, but if not, can you show me how to do with jquery if you can.

2 answers

4


With:

file_get_contents('http://api.promasters.net.br/cotacao/v1/valores?moedas=USD&alt=json');

Can’t, because that url is blocking by user_agent but we can fake it. Try so:

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/ ) ) )
  • 1

    That’s how it worked @Miguel, thank you very much!

  • You’re welcome @Raylansoares

  • 1

    Only one note, file_get_contents supports yes request handling to use headers, such as user-agent, in the case of an example: $options = array(
 'http' => array(
 'method' => 'GET',
 'header' => "Accept-language: en\r\n" .
 "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\r\n" )
);

$context = stream_context_create($options);
$file = file_get_contents('http://api.promasters.net.br/cotacao/v1/valores?moedas=USD&alt=json', false, $context);, if you can update the response ;)

  • @Guilhermenascimento I know, http://answall.com/a/192669/5749 but at the time I didn’t have the resources to test with file get Contents and so I only presented with Curl

  • @Guilhermenascimento is true, but I didn’t even remember this one

0

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;
  • I didn’t understand how it works, I even tried to replace the URL with the one I will use but it didn’t work. Could explain me better?

  • do this only for $Response = file_get_contents("http://seulink"); print_r($Response);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.