Hello I have a successful cURL request that returns the 8 largest cities in a country including the city name, country code, latitude and longitude ($largeCities['records']
). What I then want to do is loop through that array and pass the latitude, longitude and country code of each city to a new cURL routine (geonames findNearbyWikipedia api) and return that data to use in my JavaScript. I'm wondering if this is possible and if so where am I going wrong. Currently when I run the $.ajax request on my php file I'm just getting an empty array cityCluster: Array(0)
relevent php code:
//large cities in country
$url='https://public.opendatasoft.com/api/records/1.0/search/?dataset=geonames-all-cities-with-a-population-1000&q=&rows=8&sort=population&facet=timezone&facet=country&refine.country_code='. $countryCodeA2;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$largeCities = json_decode($result,true);
$output['data']['largeCities'] = $largeCities['records'];
foreach ($largeCities['records'] as $key => $value) {
$cityLat = $value['geometry']['coordinates'][0];
$cityLng = $value['geometry']['coordinates'][1];
$countryCodeISO2 = $value['fields']['country_code'];
//wiki cities clusters
$url='http://api.geonames.org/findNearbyWikipediaJSON?formatted=true&lat=' . $cityLat . '&lng=' . $cityLng . '&country='. $countryCodeISO2 .'&maxRows=15&username=' . $geoUserName . '&style=full';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$cityCluster = json_decode($result,true);
};
//output status
$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "success";
$output['status']['executedIn'] = intval((microtime(true) - $executionStartTime) * 1000) . " ms";
$output['data']['cityCluster'] = $cityCluster['geonames'];
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($output);
question from:
https://stackoverflow.com/questions/66049652/trying-to-loop-through-array-and-pass-values-to-php-curl-request 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…