1

I am reading in a file with some address information. Some of the addresses are unprocessable (error code 422). How can I return the results of the addresses that are processable and leave the error locations blank?

    require 'myFunctions.php';
    require_once '../../vendor/autoload.php';
    use Stanley\Geocodio\Client;

    $client = new Client('Your API key');

    //Read in address data.
    $AddressData = file('address.txt');

    //Send a request to the API.
    $location = $client->post($AddressData);

Screenshot of error

Illogical character

gente002
  • 63
  • 1
  • 9

1 Answers1

1

I think the API is automatically handling the errors and leaving them blanked, no? (would help if you shared a screenshot of the error) - But anyway if what you meant is to catch such errors (exceptions) here is the list of all possible exceptions in Geocodio (source):

An HTTP 403 error raises a GeocodioAuthError
An HTTP 422 error raises a GeocodioDataError and the error message will be reported through the exception
An HTTP 5xx error raises a GeocodioServerError
An unmatched non-200 response will simply raise Exception

In your case, for a non processable address you should catch the GeocodioDataError exception. Buy anyway it's a good practice to catch all the supported ones (Catch 'Em All). I guess you should be able to do something like this:

try
{
  $location = $client->post($AddressData);
}
catch(\Stanley\Geocodio\GeocodioAuthError)
{
  // do something
}
catch(\Stanley\Geocodio\GeocodioDataError)
{
  // do something
}
catch(\Stanley\Geocodio\GeocodioServerError)
{
  // do something
}
catch (\Exception $e)
{
  // do something
}

In the attached screenshot I see you are using Guzzle! In Guzzle you can catch exceptions like this (more read):

try
{
  $location = $client->post($AddressData);
}
catch (Guzzle\Http\Exception\BadResponseException $e)
{
  // do something
}
Community
  • 1
  • 1
hatef
  • 5,491
  • 30
  • 43
  • 46
  • Added the screenshot. – gente002 Feb 25 '17 at 19:12
  • Ok so now that I have the syntax for handling the error, how would I go about extracting the lat/longs for the locations that did not fail? Apologies if this is a dumb question. This is the very first API I have worked with. Thanks for your help! – gente002 Feb 26 '17 at 19:06
  • @gente002 please have a look at [docs](https://geocod.io/docs/#batch-geocoding). The library returns a JSON that contains lat/long. Just have a look at the example response in the link above and see how you can read a JSON object in PHP. You can find the lan/long under location. e.g: `"location": { "lat": 38.886665, "lng": -77.094733 },` – hatef Feb 28 '17 at 11:31
  • Sorry I probably didn't not ask the question very clearly. I understand how to get the lat/long from the response as long as the request array does not cause any errors. However, when I send a larger array that contains at least 1 address that responds as a 422 (unprocessable entity), it gives me the error that I shared. Now, I can use your try/catch code to catch the error but now I am just back to trying to make the call to the api with that same array. I did try a loop method, which sends each location individually but this defeats the purpose of the "bulk" request. – gente002 Feb 28 '17 at 12:48
  • So, in summary, I want to send an array to the API that contains some "422 error" addresses but I still want the api to send the response with the locations that it can. I notice the $e array in your example but I don't think that this contains what I need. – gente002 Feb 28 '17 at 12:50
  • 1
    I figured out what my issue was. In my text file there was an illogical character throwing the request off. I understand why this was confusing to you now. Screenshot above. Thanks again for your help. – gente002 Mar 04 '17 at 01:59
  • @gente002 ah that's cool that you found out the reason! Actually I have never used this API :D - glad to have helped anyway. – hatef Mar 04 '17 at 10:37