I am trying to create a simple too to get WalkScores for various coordinate pairs using the API. I have two different UI. One has two input text areas, one for the lats and one for lons. It works fine but this is more work for the user. The second UI has each coordinate pair on each line of a text area. The first script below works while the second gives errors even through they use the same functions.
Working script. This takes two fields from a form and creates the arrays to use in the loop. This works but not idea.
<?
$apiKey = 'a55fc4524343bfc9cdd901361092197c';
//comes from form
$lat = $_POST['lat'];
$lon = $_POST['lon'];
// for testing without UI
// $lat = "41.767679,41.346483";
// $lon = "-72.680463,-73.251384";
// for testing without UI
$lats = explode(',', $lat);
$lons = explode(',', $lon);
$address = 'unknown';
for ($i = 0; $i < count($lats); $i++) {
$json = getWalkScore($lats[$i], $lons[$i], $address);
$json_walkScore = json_decode($json);
echo $lats[$i] . "\r\n";
echo $lons[$i] . "\r\n";
echo $json_walkScore->walkscore . "\r\n";
echo $json_walkScore->description . "\r\n";
}
function getWalkScore($lat, $lon, $address)
{
$address = urlencode($address);
$url = "http://api.walkscore.com/score?format=json&address=$address&lat=$lat&lon=$lon&wsapikey=a55fc4524343bfc9cdd901361092197c";
$str = @file_get_contents($url);
return $str;
}
Not working This looks to create identical arrays for use in the loop but returns an error "Notice: Trying to get property 'walkscore' of non-object in..." and only returns the expected output for the last iteration. It works fine for one pair. If I use the hardcoded arrays it works fine..
<?
$apiKey = 'c93ad8c6751f03a31889414b680eec49';
//Data sent from Ajax
$input = $_POST['input'];
//Split string based on new line from text area. Each item is on its own line.
$arr = explode("\n", $input);
//Two arrays to hold data
$lats = [];
$lons = [];
//Break each pair into lat lng and push to arrays
foreach ($arr as $k => $v) {
$coord = explode(",", $v);
$lat = $coord[0];
$lon = $coord[1];
array_push($lats, $lat);
array_push($lons, $lon);
}
//using these hardcoded arrays it works fine.
// $lats = ["41.113676","41.104206"];
// $lons = ["-73.439233","-73.404587"];
//using these hardcoded arrays it works fine.
//Set address to unknown since it is not provided / needed
$address = 'unknown';
//loop over the number of lats, run the get walk score function to return the json data for output
for ($i = 0; $i < count($lats); $i++) {
$json = getWalkScore($lats[$i], $lons[$i], $address);
$json_walkScore = json_decode($json);
// print_r($json_walkScore);
echo $lats[$i] . "\r\n";
echo $lons[$i] . "\r\n";
echo $json_walkScore->walkscore . "\r\n";
echo $json_walkScore->description . "\r\n";
}
function getWalkScore($lat, $lon, $address)
{
$address = urlencode($address);
$url = "http://api.walkscore.com/score?format=json&address=$address&lat=$lat&lon=$lon&wsapikey=a55fc4524343bfc9cdd901361092197c";
$str = @file_get_contents($url);
return $str;
}
Any ideas on why the first is working but the second is not? Let me know if you have questions. Thanks!
After more testing this line
$arr = explode("\n", $input);
needs to be changed to this
$arr = explode("\r\n", $input);
fun... Same EXACT output but it the code decides to work this time.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.