Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I need some type of PHP thingie so that I can input a few street-addresses
(such as:
123 Fake Street,
Faketown, FA,
98765)
and then it will give me the coordinates (latitude and longitude) of these addresses.
I need this to be completely dynamic (as I will be fetching these addresses from a DataBase).
Where can I find something that can do this?
The process of taking an address and turning it into lat/lng is called "Geocoding". I would look at the Google Geocode API.
https://developers.google.com/maps/documentation/geocoding/
I work for SmartyStreets, an address verification API provider. Our LiveAddress API does just what you need. An account is free to setup and it comes with 250 lookups per month. Here's some sample PHP code:
<?php
// Customize this (get ID/token values in your SmartyStreets account)
$authId = urlencode("raw ID here");
$authToken = urlencode("raw token here");
// Address input
$input1 = urlencode("3785 s las vegs av.");
$input2 = urlencode("los vegas,");
$input3 = urlencode("nevada");
// Build the URL
$req = "https://api.smartystreets.com/street-address/?street={$input1}&city={$input2}&state={$input3}&auth-id={$authId}&auth-token={$authToken}";
// GET request and turn into associative array
$result = json_decode(file_get_contents($req),true);
echo "<pre>";
print_r($result);
echo "</pre>";
?>
What comes out looks like this:
{
"input_index": 0,
"candidate_index": 0,
"delivery_line_1": "3785 Las Vegas Blvd S",
"last_line": "Las Vegas NV 89109-4333",
"delivery_point_barcode": "891094333992",
"components": {
"primary_number": "3785",
"street_name": "Las Vegas",
"street_postdirection": "S",
"street_suffix": "Blvd",
"city_name": "Las Vegas",
"state_abbreviation": "NV",
"zipcode": "89109",
"plus4_code": "4333",
"delivery_point": "99",
"delivery_point_check_digit": "2"
},
"metadata": {
"record_type": "H",
"zip_type": "Standard",
"county_fips": "32003",
"county_name": "Clark",
"carrier_route": "C024",
"congressional_district": "01",
"building_default_indicator": "Y",
"rdi": "Commercial",
"elot_sequence": "0119",
"elot_sort": "A",
"latitude": 36.10357,
"longitude": -115.17295,
"precision": "Zip9"
},
"analysis": {
"dpv_match_code": "D",
"dpv_footnotes": "AAN1",
"dpv_cmra": "N",
"dpv_vacant": "N",
"active": "Y",
"footnotes": "B#H#L#M#"
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
My API call returns the following JSON output before json_decode:
{
"projects": [
{
"project_id": 00000001,
"name": "A title",
"price": "0.99",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "A text of a comment"
}
{
"project_id": 00000002,
"name": "Another title",
"price": "1.03",
"country": "US",
"platform_types": [
"android_phone",
"ios_phone",
"ios_tablet",
"android_kindle",
"android_tablet",
"desktop"
],
"comment": "Another text of a comment"
}
]
}
The following code parses the multi-level json and shows the whole projects list:
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
foreach($result['projects'] as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
$country = $project['country'];
#no values for the fields, that's why commented
#$android_phone = $project['platform_types']['android_phone'];
#$ios_phone = $project['platform_types']['ios_phone'];
#$ios_tablet = $project['platform_types']['ios_tablet'];
#$android_kindle = $project['platform_types']['android_kindle'];
#$android_tablet = $project['platform_types']['android_tablet'];
#$desktop = $project['platform_types']['desktop'];
$comment = $project['comment'];
echo $project_id,'<br>',$name,'<br>',$price,'<br>',$country,'<br>',$comment,'<br>';
}
I got the following output:
00000001
A title
0.99
US
A text of a comment
00000002
Another title
1.03
US
Another text of a comment
The questions are:
How to list available device types (the fields have names only and no values)?
How to filter the array based on certain criteria (price must be equal or higher $1.00)?
How to filter elements based on fields without values (show projects just for Android devices)?
Question 1
to list all the available device types per project as a json you should access it's corresponding tree
foreach($result['projects'] as $project) {
$project_device_types = $project['plateform_type'];
echo '<pre>'.$project_device_types.'<pre>';
}
Question 2
to filter the array use [array_filter][1] in
$filtered = array_filter($result['projects'], function($project) {
if($project['price'] >= 90) {
return true
}
});
// use it instead of the $result variable
foreach($filtered as $project) {
$project_id = $project['project_id'];
$name = $project['name'];
$price = $project['price'];
}
Question 3
you can follow the exact pattern as the second question and that is by using array_filter so eg. let's say you want only the android devices
array_filter($result['projects'], function($project) {
if(in_array($project['platform_types'], "Android")) { // check if the plateform type include android by using [in_array][1]
return true
}
});
NB: in_array is case sensitive so make sure that the capitalization is correct
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
{
"data": {
"orderId": "10946506",
"status": "FAILED",
"mobile": "9795681183",
"amount": "100.00",
"transId": "",
"error_code": "119",
"service": "Airtel",
"bal": "",
"creditUsed": "0",
"resText": "Ip 185.27.134.67 Not Configured",
"gstMode": "P2P",
"tdsAmount": "0"
}
}
You can do that using PHP native function json_decode, here the example :
<?php
$json ='{"data":{"orderId":"10946506","status":"FAILED","mobile":"9795681183","amount":"100.00","transId":"","error_code":"119","service":"Airtel","bal":"","creditUsed":"0","resText":"Ip 185.27.134.67 Not Configured","gstMode":"P2P","tdsAmount":"0"}}';
$result = json_decode($json);
//Array
print_r($result);
//single data
echo $result->data->orderId;
?>
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
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What I have :
I have a software dump data like exactly below file :
DMPDATA = {
["invent1"] = {
["1000:1"] = {
["I"] = "6948",
["C"] = 1,
["G2"] = "0",
["G3"] = "0",
["G1"] = "0",
},
["0000:10"] = {
["I"] = "39622",
["C"] = 1,
["G2"] = "0",
["G3"] = "0",
["G1"] = "0",
},
},
["invent2"] = {
["M:1"] = 60116,
["M:3"] = 32246,
["M:2"] = 41252,
},
["invent3"] = {
["47465"] = 5,
["12970"] = 5,
},
["invent4"] = {
{
["F"] = 0,
["V"] = 0,
["N"] = "Classic",
}, -- [1]
{
["F"] = 16,
["V"] = 3500,
["N"] = "Horde",
}, -- [2]
},
["invent6"] = {
["class"] = "WARRIOR",
["gender"] = 2,
},
}
Question:
I want to parse above data as array , I try to do but don't know whats better way .
How can parse files like above code with PHP to have a all data as Array ?
This looks like LUA code. Have you tried the Lua class in PHP?
http://www.php.net/manual/en/lua.eval.php
Here's a guy that has a similar problem with a WoW Addon Lua file:
I need a tool to parse Lua tables, preferrably in Ruby or Java
EDIT:
Try this tool. It also links to a PHP script you might use. http://fin.instinct.org/lua/
EDIT 2:
This is basically what you need. As you can see you're not the first person who wants to parse WoW Lua dumps to PHP arrays =)
http://fin.instinct.org/lua/lua2phparray.phps