Calling an API from a wordpress page

Hello All: I am hoping someone can help me with this. I have never done a call to an API and am really out of my depth. What I am trying to do is a call to an API on a wordpress page. Here is what I have from the API:
http://zipcodedistanceapi.redline13.com/rest//distance.///

I have an API key. I have been trying to learn how to do this but there are thousands of ways and am really at a loss how to do this on a wordpress page. Can anyone help?

Read More

Thanks so much, Gerard

Related posts

Leave a Reply

1 comment

  1. You can do it in php (here I use file_get_contents):

    $distance = file_get_contents("http://zipcodedistanceapi.redline13.com/rest/<api_key>/distance.<format>/<zip_code1>/<zip_code2>/<units>");
    

    Example taken from their site:

     http://zipcodedistanceapi.redline13.com/rest/qVuOZidNAe5osYgYHbyK33EHpJD2nKjOFODE6QsH5y6yxnxfy5ZC9DjynXBLHUAm/distance.json/1000/2000/km
    

    You can also use cURL or anything which can call an external page.

    To decode json, have a look on json_decode.

    Think about caching the values using your database or user cookies if possible to avoid multiple unnecessary requests.

    EDIT: Based on the code provided in your comment (I took a fake API key from demonstration purposes):

    $distance = file_get_contents("http://zipcodedistanceapi.redline13.com/rest/qVuOZidNAe5osYgYHbyK33EHpJD2nKjOFODE6QsH5y6yxnxfy5ZC9DjynXBLHUAm/distance.json/48433/485‌​01/miles"); 
    print_r(json_decode($distance));