This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I'm looking to create a basic IP tracker, I'm trying to figure out how to echo certain aspects of this variable, cheers:
{"ip":"MY IP","country_code":"GB","country_name":"United Kingdom","region_code":"ENG","region_name":"England","city":"MY CITY","zip_code":"MY CODE","time_zone":"Europe/London","latitude":----,"longitude":-----,"metro_code":-}
This is the code which I was reading up on:
$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
print_r($location);
{
"ip": "77.99.179.98",
"country_code": "GB",
"country_name": "United Kingdom",
"region_code": "H9",
"region_name": "London, City of",
"city": "London",
"zipcode": "",
"latitude": 51.5142,
"longitude": -0.0931,
"metro_code": "",
"areacode": ""
}
Cheers!
To echo something do
//Example
$locationObject = json_decode($location);
echo $locationObject->ip;
//Prints out: 77.99.179.98
More info
http://www.dyn-web.com/tutorials/php-js/json/decode.php
Related
im trying to do this json decoding my professor suggested i try, but i cant seem to pass the address part. Any ideas?
{"fname": "Nick", "mname": "F.", "lname": "Delos Reyes", "birthday": "1995-04-01", "address": { "Country": "America", "state": "New York" }}
Expected output:
Name : Nick F. Delos Reyes
Birthday : 1995-04-01
Address :Caramoran, New York
There are many ways to it. Following is an example to parse the name.
$array = json_decode('{"fname": "Nick", "mname": "F.", "lname": "Delos Reyes", "birthday": "1995-04-01", "address": { "Country": "America", "state": "New York" }}', true);
$name = [$array['fname'], $array['mname'], $array['lname']];
$outputName = 'Name:' . implode(' ', $name);
It uses json_decode() to convert the JSON to an associative array. It then put the name parts in an array. It uses implode() to join the name parts into a string.
This question already has answers here:
PHP replace wildcards (%s, %d) in string with vars
(2 answers)
Closed 3 years ago.
I've got a JSON file which looks like this
{
"facebook": {
"icon": "fab fa-facebook",
"title": "Facebook",
"url": "https://facebook.com/%s"
},
"instagram": {
"icon": "fab fa-instagram",
"title": "Instagram",
"url": "https://instagram.com/%s"
}
}
So I'm getting users social links from a form, but only the user's ID of social link eg.https://facebook.com/ID. I'm storing the users ID in JSON file in database. I'm using PHP. How do I add the users ID in that '%s' and display the link.
To put together the information using the JSON data you have, you would use either sprintf() or printf() (the only difference being the printf() directly outputs the data sprintf() returns a string). The information on the manual pages shows how things like %s works.
So the code would look something like...
$id = 123;
$userName = "User name";
$json = '{
"facebook": {
"icon": "fab fa-facebook",
"title": "Facebook",
"url": "https://facebook.com/%s"
},
"instagram": {
"icon": "fab fa-instagram",
"title": "Instagram",
"url": "https://instagram.com/%s"
}
}';
$socialMedia = json_decode( $json, true );
echo echo '<a href="'.sprintf($socialMedia["facebook"]["url"], $id).'">'.
$userName.'</a>';
Which outputs...
User name
I've read through all the questions I could find on here, and but none of the examples I've found have worked, current have a script returning the following json:
{
"clickid": "24231527",
"geo_data": {
"country_code": "FR",
"state": "Paris",
"city": "Paris",
"currency_symbol": "\u0080",
"currency_code": "EUR"
}
}
I've tried all of the following, but I keep getting either a square box, or question mark when I try to convert it to UTF-8:
header('content-type:text/html;charset=utf-8');
$data = json_decode($output, false, JSON_UNESCAPED_UNICODE);
$geo_data->{"currency_symbol"} = utf8_encode($geo_data->{"currency_symbol"});
Every combination I try to get it to print the € is either returning a box (withont the encode) or a ? (with the encode).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this JSON:
{
"cache": true,
"data": [
{
"unique_id": "914239",
"description": "New Zealand 370/10 & 335/10 v Australia 70/1 & 505/10 *",
"title": "New Zealand 370/10 & 335/10 v Australia 70/1 & 505/10 *"
},
{
"unique_id": "973833",
"description": "Helmand Province Under-17s 135/10 * v Khost Province Under-17s 286/9 ",
"title": "Helmand Province Under-17s 135/10 * v Khost Province Under-17s 286/9 "
},
{
"unique_id": "935949",
"description": "Mid West Rhinos v Mashonaland Eagles 264/2 *",
"title": "Mid West Rhinos v Mashonaland Eagles 264/2 *"
},
{
"unique_id": "973379",
"description": "Mountaineers 136/10 v Matabeleland Tuskers 42 *",
"title": "Mountaineers 136/10 v Matabeleland Tuskers 42 *"
},
{
"unique_id": "959221",
"description": "Islamabad United v Quetta Gladiators",
"title": "Islamabad United v Quetta Gladiators"
}
],
"provider": {
"pubDate": "2016-02-23T14:01:01.467Z",
"source": "http://www.cricinfo.com/",
"url": "http://crm.wherrelz.com/"
}
}
and I need to display title and data in my PHP website.
Quite simple :
$raw_json = file_get_contents('data.json');
$array = json_decode($raw_json);
foreach ($array->data AS $data) {
echo $data->title;
echo '<br />';
echo $data->description;
echo '<br />';
}
Where of course the file data.json contains your json.
You can decode the data with json_decode you can find the full docs here: http://php.net/manual/en/function.json-decode.php
In terms of an example piece of code, you could do something like the following:
$decoded = json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) {
// Do something when you don't have valid json.
}
foreach ($decoded->data as $data) {
echo $data->unique_id;
echo $data->title;
}
json_last_error() will allow you to catch any invalid JSON or any other potential errors that json_decode() may come across, so I'd definitely reccomend using it and ensuring you have decoded valid json before doing anything with it.
{
"name": "2Pac",
"album": "All Eyez on Me",
"track": "All Eyez on Me",
"duration": "5:08",
"trackNo": ""
},
{
"name": "2Pac",
"album": "2Pac Greatest Hits",
"track": "Keep Ya Head Up",
"duration": "4:24",
"trackNo": "1"
},
{
"name": "2Pac",
"album": "2Pac Greatest Hits",
"track": "2 Of Amerikaz Most Wanted",
"duration": "4:07",
"trackNo": "2"
},
{
"name": "50 Cent",
"album": "Get Rich Or Die Tryin%27",
"track": "P.I.M.P.",
"duration": "4:10",
"trackNo": "11"
},
{
"name": "50 Cent",
"album": "Get Rich Or Die Tryin%27",
"track": "Like My Style",
"duration": "3:13",
"trackNo": "12"
},
I have lots of json data formatted this way and was looking for a way to manipulate the data so that it only has one artist name and then the albums for that artist then songs within each album. PHP would be fine so I can convert things, Any help would be appreciated.
Use these:
json_decode
array_map, array_filter, for loops, what you like
json_encode
Basically it boils down to:
parse
mangle
format
I'll leave the exercise to you, but browsing these functions and similar questions here should get you started. If you have anything to show then, you may ask for more specific help.