Need your help. I have a json array:
ethereumStats = {"blockTime": 16.890625, "lastUpdate": 1497042002.725094, "priceUsd": 275.742, "difficulty": 663594472929704.8};
How can i get the result of each data from array.
i tried this way:
$info = file_get_contents("http://alpha61.com/ethereum.json");
$result = json_decode($info,true);
$blockTime = $result["priceUsd"];
But it does'n work.
Here's something I threw together to parse out the JSON string from the file that is returned:
$info = file_get_contents("http://alpha61.com/ethereum.json");
$data = explode(" = ", $info);
$json = rtrim($data[1],";");
$result = json_decode($json,true)
$blockTime = $result["priceUsd"];
The file you are requesting was meant to be used in a javascript tag, like this <script src="http://alpha61.com/ethereum.json"></script>, so its a little awkward to pull the JSON out of it for use in php
Related
I have this simple code on my site:
$info = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
which displays like this:
{"ip":"123.123.12.1","country_code":"GB","country_name":"United Kingdom","region_code":"ENG","region_name":"England","city":"London","zip_code":"LN1","time_zone":"Europe/London","latitude":30.302,"longitude":-1.123,"metro_code":0}
I am trying to turn this in to an array so that I can import this data to the database using something similar to:
$ip_address = $info['ip'];
$city = $info['city'];
And so on... I have attempted to use $info[1], $info[2] and it just displays { " each character of text. Is there a simple way to do this?
That is a JSON format and you can use the json_decode() function to convert it to either object or array.
So you have:
$info = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
$info = json_decode($info, true);
Then you can use, as you said, the $info['ip'] and $info['city']
Use json_decode
$info_obj = json_decode($info);
Now $info_obj has the info you want, and you can access it this way:
$ip_address = $info_obj->ip;
$city = $info_obj->city;
I'm trying to create a very simple php script that can pull data from a JSON file (array?) and echo it to the page. Sadly, I'm a complete newbie when it comes to PHP.
My goal is to dump all IP addresses and the corresponding client version into an output like this...
"127.0.0.1" "/Satoshi:0.9.1/"
"127.0.0.2" "/Satoshi:0.9.0/"
"127.0.0.3" "/Satoshi:0.9.0/"
"127.0.0.4" "/Satoshi:0.9.1/"
I can get the code to dump all data, but I'm not sure how to pull the ip and version without the ip and client version being named.. If that even makes sense?
Here is the code. What do I need to make it dump the correct data?
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$JSON = file_get_contents($url);
echo $JSON;
$data = json_decode($JSON);
var_dump($data);
?>
Thanks for your help!
Something like
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$JSON = file_get_contents($url);
$data = json_decode($JSON);
$data = (array)$data; // cast from stdClass to array, as results have int keys
foreach($data['nodes'] as $results){
echo $results[0]. " ". $results[3]."<br />";
}
?>
It doesn't makes much sense to convert an array of hashes into the one of some non-named entities. I guess that all you need is to dump it in the following way:
foreach($data as $t)
{
echo("ip=".$t->ip." ua=".$t->ua);
}
I don't think there is any reason to cast to an array. Just process the returned object. This one has the double quotes as requested as well.
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$json = file_get_contents($url);
$data = json_decode($json);
foreach($data->nodes as $results){
echo "\"".$results[0]."\" \"".$results[3]."\"<br />";
}
?>
I'm able to access my $_POST array values, but can't seem to get the correct syntax for returning only the "data" part of the array. If I run var_dump($_POST["attributes"]);, I receive the following in the response callback(which is exactly what I expected):
string '{
"device_maker" = unknown;
"device_model" = Simulator;
"first_visit" = "1387478168.109";
"last_visit" = "1388358490.638";
latitude = "37.78583526611328";
locale = en;
longitude = "-122.4064178466797";
"opted_in" = 1;
"opted_out" = 0;
"os_platform" = "iPhone OS";
"os_version" = "7.0.3";
"this_visit" = "1387478168.109";
"user_id" = 1;
}' (length=389)
If I try to access any one of the attributes separately, like var_dump($_POST["attributes"]["device_model"]);, all I get in return is string '{' (length=1). I'm apparently missing a key idea on parsing this data. How do I parse "attributes" so I can place each one of the listed values into an insert statement(I've got that part ready to go once I get the data)? Granted, my php is very rusty. So I may be overlooking something obvious.
It's frustrating to see the data I need and not know how to correctly access it. So, any help is appreciated. Please ask if you need clarification.
Apparently the string being retrieved in your POST is not valid JSON. If you can't replace the values being sent to the server, you can always do a (dirty) workaround:
First, replace some characters to make it a valid json:
$jsonStr = str_replace('=', ':', $_POST["attributes"]);
$jsonStr = str_replace(';', ',', $jsonStr);
$jsonStr = str_replace(',}', '}', $jsonStr);
Then we can try to parse it using the json_decode function:
$jsonArray = json_decode($jsonStr);
Now you can access it as a regular associative array, e.g.:
echo $jsonArray['device_model'];
What you show here looks like JSON encoded data - it's a single string so there is no $_POST["attributes"]["device_model"].
You can access the data embedded in the attributes variable by converting it from JSON:
$a_string='{
"device_maker" = unknown;
"device_model" = Simulator;
"first_visit" = "1387478168.109";
"last_visit" = "1388358490.638";
latitude = "37.78583526611328";
locale = en;
longitude = "-122.4064178466797";
"opted_in" = 1;
"opted_out" = 0;
"os_platform" = "iPhone OS";
"os_version" = "7.0.3";
"this_visit" = "1387478168.109";
"user_id" = 1;
}';
// or $astring=$_POST['attributes']
$data=json_decode($a_string);
print $data['device_model'];
....but you need to verify the origin of the data - if it' not JSON, then this will break at some point.
http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json
is the url, which contains json data. I want to use that data and send SMS to a particular phone no if value of latitude and longitude(Extracted from JSON).
Check constraints, we can use through php. But the main problem is How to extract data from JSON file?
I don't want to give you the solution, so the below should be enough to get you started.
Read in the JSON data using file_get_contents
Parse the JSON string into a PHP object using json_decode
Your code should look something like this:
$url = "http://www.instamapper.com/api?action=getPositions&key=584014439054448247&num=10&format=json";
$contents = file_get_contents($url);
$jsonObj = json_decode($contents);
You mean something like this?
<?php
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->trends as $trend )
{
echo "{$trend->name}\n";
}
require_once 'include/BestBuy/Service/Remix.php';
$skuid = rawurldecode($_GET['skuid']);
$apiKey = 'tfvs7h89pwn4pzmyj9nxemmg'; // Your API key
$remix = new BestBuy_Service_Remix($apiKey);
$result = $remix->product('$skuid','json')
->show(array('url'))
->query();
$data = json_decode ($result, true);
$feed = $data['url'];
print <<< FEEDS
$feed
FEEDS;
When I put this script into my page, the $feed will echo the current URL.
But when I manually supply the script with an integer, replacing ($skuid) it will be successful.
It's really weird, But I think it has something to do with me using a variable in that specific array.
And it is also weird, because It was working before I re arranged some of the HTML.
I'm trying to approach this problem the most logical way.
Please help.
Thanks.
should you have $skuid in quotes? I would expect:
$result = $remix->product($skuid,'json')
rather than
$result = $remix->product('$skuid','json')