Reputation: 549
I trying to get json data from website. In the first link, it returns blank. But in the second, it returns json.
Someone know how I could to do to get json data from the first link?
Before access this code, please login in this link: https://loginfree.globo.com/login/438 user: [email protected] pass: cartoletasfc
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<iframe src="http://cartolafc.globo.com/time/claudioivp-allianz/atletas.json?rodada_id=12"></iframe>
<?php
$file = json_decode(file_get_contents("http://cartolafc.globo.com/time/claudioivp-allianz/atletas.json?rodada_id=12"));
print_r($file);
echo "casa";
?>
<iframe src="http://cartolafc.globo.com/mercado/filtrar.json?page=1&order_by=preco&clube_id=277"></iframe>
<?php
$file = json_decode(file_get_contents("http://cartolafc.globo.com/mercado/filtrar.json?page=1&order_by=preco&clube_id=277"));
print_r($file);
echo "casa";
?>
</body>
</html>
Upvotes: 0
Views: 860
Reputation: 4338
Well apparently the first json URI requires the user to be logged in the website otherwise it redirects you to the homepage and the second one appears to be open to everyone.
The only solution I see here is to use curl (or any other method you'd like) to emulate a browser and follow the login process for that website, sending in your login, password and any other info that it requires. Then you can catch the cookies that it will generate. After you get the cookies you can keep then stored in a cookiejar file or a database.
With these cookies you'll be able to retrive the json file, you can send them through a file_get_contents
by using a stream_context
object or if you're using curl there is the CURLOPT_COOKIE
and CURLOPT_COOKIEJAR
Of course these cookies will expire. Just set-up a verification process in which it gets new cookies if the stored ones have expired.
If the contents of this json file do not update with a high frequency or if it's not really important for the users to get these updates right away you could just keep a copy of this json file in your server without any access restriction.
It would be silly but in case that domain is YOURS (or you have access to it) just cut out the login verification in that file??
Upvotes: 1