0

I've searched and found numerous posts that seem to address this but none seem to work for me. I'm hoping someone with real experience (I'll admit I'm just a hack at this stuff) can point me in the correct direction.

I'm pulling data from an FCC API. Their URL is

https://geo.fcc.gov/api/census/area?lat=47.91631&lon=-120.19647&format=json

Its purpose is to submit a LAT and LON to retrieve State and County info.

It returns JSON as:

{
  "input": {
    "lat": 47.91631,
    "lon": -120.19647
  },
  "results": [
    {
      "block_fips": "530079604003023",
      "bbox": [
        -120.197547,
        47.90941,
        -120.190388,
        47.920194
      ],
      "county_fips": "53007",
      "county_name": "Chelan",
      "state_fips": "53",
      "state_code": "WA",
      "state_name": "Washington",
      "block_pop_2015": 25,
      "amt": "AMT007",
      "bea": "BEA169",
      "bta": "BTA468",
      "cma": "CMA694",
      "eag": "EAG006",
      "ivm": "IVM694",
      "mea": "MEA046",
      "mta": "MTA024",
      "pea": "PEA206",
      "rea": "REA006",
      "rpc": "RPC005",
      "vpc": "VPC007"
    }
  ]
}

I want to get the "county_name" and "state_code" fields. Here is the code I've plagiarized and am trying to make work.

...
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://geo.fcc.gov/api/census/area?lat=47.91631&lon=-120.19647&format=json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

$response = json_decode($response, true); //because of true, it's in an array
echo "County & State: ". $response['county_name']['state_code'];
...
  • almost, `$response['results'][0]['county_name']`, then do same for `['state_code']` – Lawrence Cherone Jul 10 '20 at 11:14
  • I'm sorry, but I must be an idiot. I modified the code as follows and still don't get a response. Is there some kind of error checking I could add to help narrow it down? $response = json_decode($response, true); //because of true, it's in an array $CountyName = $response['results'][2]['county_name']; $StateCode = $response['results'][4]['state_code']; echo "County: ". $CountyName . " - State: " . $StateCode; The echo line shows the text but the variables don't display anything. If you try this same code in a test PHP file does it work for you? – Lenny Gemar Jul 11 '20 at 03:14
  • there is no `[1]`, `[2]` etc.. do a `print_r($response)` and then traverse it – Lawrence Cherone Jul 11 '20 at 12:30
  • Lawrence, thank you for the print_r tip. It allowed me to locate an issue with the URL and I have it all working as designed. I appreciate your help and your patience. Have a great day! Thanks! – Lenny Gemar Jul 12 '20 at 01:10

0 Answers0