1

I'm trying to print the distance in km between two ZIP codes, using this Zip Code API.

My code:

$settings = json_decode(file_get_contents('https://www.zipcodeapi.com/rest/rDJ7VmDHWP7NVwCXRbznkauB5DddKBQbeAjaWXSSM0FIxiFoueMPohquUDIFfzQt/distance.json/45410/45412/km'), true);
print_r($settings['distance']);

When I visit the actual link, it returns the expected result of 3.635 km. However, the code actually prints out seemingly random numbers, such as 17.04, 90.79, and 46.33 (my last 3 attempts).

I am running this on Cloud9, I have tried isolating it on a new page.

Am I I using json_decode wrong or does this seem like an issue with the API?

5
  • What does your data look like? Commented Aug 23, 2017 at 17:28
  • 4
    When I run that, I see 3.635. Commented Aug 23, 2017 at 17:28
  • I have run it on a new page in Cloud9, because I do not have a web server set up to execute the PHP code. Here is the preview link Commented Aug 23, 2017 at 17:34
  • 1
    I ran it and got the expected result, so then changed the zip codes to ones I know, and the results were way off. This API is not accurate for determining distance between two zips. It's probably not your code, it's the API causing problems. Commented Aug 23, 2017 at 17:37
  • 1
    @JakeGould, I added a preview link when the code is isolated in my above comment, which only contains the code in my question plus <br><b>test</b>. Visiting the actual site gives you correct info, but I just discovered that getting requests from the API without registering on their site will generate random numbers. Quite silly, but once I registered it worked with my new key. Commented Aug 23, 2017 at 17:53

1 Answer 1

2

It appears that the API I was using requires you to register an API key in order to use it properly, or else it will generate random values. I registered and it only allows up to 50 requests per hour with the free key, but it worked once I used the key from registering.

For those that are looking for an API with a higher request/day limit, Google's API works well also.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.