Google Geocoding API Example in PHP

I had to quickly create a script to set the latitude and longitude value in a DB knowing the address.

I googled and found this site that explained the basics of how to use the Google Geocoding API.

I decided to expand on his example and create a script that shows one how to get the individual address components and save them in an associative array.  Saving them in an associative array makes it a bit cleaner.  I guess a better way can be to save it in a class and use get functions for the cleanest solution.  I’ll leave that to the next person.  Here is my example on my github page.


$addr = "1600 Pennsylvania Avenue, 20500";

$result = geocode($addr);


echo "The full address is: ".  $result['formatted_address'] 
." with latitude: ".$result['lat'] ." and longitude: ".$result['lon']." \n";

// function to geocode address, it will return false if unable to geocode address
function geocode($address){
    // url encode the address
    $address = urlencode($address);
    // google map geocode api url
    $url = "{$address}";
    // get the json response
    $resp_json = file_get_contents($url);
    // decode the json
    $resp = json_decode($resp_json, true);
    // response status will be 'OK', if able to geocode given address 
        // get the important data
		$street_number = NULL;
		$street_name = NULL;
		$city_name = NULL;
		$county_name = NULL;
		$state_name = NULL;
		$state_abbr = NULL;
		$country_name = NULL;
		$country_abbr = NULL;
        $lati = $resp['results'][0]['geometry']['location']['lat'];
        $longi = $resp['results'][0]['geometry']['location']['lng'];
        $formatted_address = $resp['results'][0]['formatted_address'];
		$address_components = $resp['results'][0]['address_components'];
		foreach ($address_components as $component) {
			$type = $component['types'][0];
			if ($type == 'street_number')
				$street_number = $component['short_name'];
			else if ($type == 'route')
				$street_name = $component['short_name'];
			else if ($type == 'locality')
				$city_name = $component['short_name'];
			else if ($type == 'administrative_area_level_2')
				$county_name = $component['short_name'];
			else if ($type == 'administrative_area_level_1') {
				$state_name = $component['long_name'];
				$state_abbr = $component['short_name'];
			else if ($type == 'country') {
				$country_name = $component['long_name'];
				$country_abbr = $component['short_name'];
			else if ($type == 'postal_code')
				$postal_code = $component['short_name'];
        // verify if data is complete
        if($lati && $longi && $formatted_address){
            // put the data in the array
            $data_arr = array();   

			$data_arr['street_number'] = $street_number;
			$data_arr['street_name'] = $street_name;
			$data_arr['city_name'] = $city_name;
			$data_arr['county_name'] = $county_name;
			$data_arr['state_name'] = $state_name;
			$data_arr['state_abbr'] = $state_abbr;
			$data_arr['country_name'] = $country_name;
			$data_arr['country_abbr'] = $country_abbr;
			$data_arr['lat'] = $lati;
			$data_arr['lon'] = $longi; 
			$data_arr['formatted_address'] = $formatted_address;
            return $data_arr;
            return false;
        return false;




Posted in Uncategorized | Leave a comment

iOS: Alamofire and SwiftyJSON

Alamofire is great to use to create an iOS app to web service.

SwiftyJSON makes parsing JSON much simpler too.

Liked this example for Alamofire

Posted in iOS | Tagged | Leave a comment

iOS Swift Tutorial

Working on iOS app these days.  Updated an old iOS app that had to old Search Bar to the new iOS 8 version of Search Bar. Objective C

Now doing POC for a new iOS app in Swift.

Not a fan of Objective C and Swift. Though Swift seems to be more like other typical programming languages.

I like this iOS Swift tutorial

Posted in iOS | Tagged , , | Leave a comment

Deleting messages in mailq

Had all these messages stuck in the mailq and couldn’t figure out how to delete the queued messages.  Turns out they were in these locations:




Ubuntu Postfix

Posted in Linux | Tagged , , , | Leave a comment

Skills wanted

Here is the technology stack I came up with:

Bootstrap Framework
Oracle (PL/SQL)
Ruby on Rails




Posted in Coding | Leave a comment

Amazon MWS feed

Found that information about Amazon’s MWS is pretty hard to find and pretty scattered.  Took me awhile to even find a link to ORDER_ACKNOWLEDGEMENT, ORDER_FULFILLMENT sample that Amazon provides.

If you are fulfilling multiple orders in one XML you want to repeat everything in the <Message>

<?xml version="1.0" encoding="iso-8859-1"?>
<AmazonEnvelope xmlns:xsi="" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">


Link to solution

Posted in Uncategorized | Tagged , , , , | Leave a comment

PHP SAP RFC Function call input not working

This is the first time I used PHP to call a SAP function.  Ran into this problem that I couldn’t figure until someone with experience helped me.

The SAP function SAP_FUNCTION had one input that expects a length of 10.

I did not realize that that meant that the input has to be a length of 10.  The examples the SAP analyst gave me had inputs with 9 and I kept trying it with 9 and was not getting anything back.

In PHP, a fellow developer told me I had to pad the input so the length is 10.

str_pad($vartopad, 10,”0″, STR_PAD_LEFT)

Spent most of the morning trying to figure out the problem and never found anything online about the need for padding.  Maybe it’s a problem with the SAP function but either way maybe it’ll help someone with input errors using PHP to connect to SAP.

Posted in Web | Tagged , , , , , | Leave a comment

Apache paths (default)

Had to look this up multiple times now:

access log (sudo):




props if figured out what I’m doing

Posted in Linux | Tagged , , , , , , , | Leave a comment

Rails Association

Spent a day learning how about associations.  Realized that has_and belongs to and the necessary join table isn’t as easy to use as creating a regular join model table and use has_many :through.

Join Table = create_join_table

Join Model Table = create_table

Following Railscast, it promoted to use join model along with has_many :through.  Part of the issue I had was that I wanted additional information in the join table like the timestamp information.  Especially for Many to Many relationships.


Originally I planned on using Rails migration to create all my tables.  After creating only 7 of them I feel like it is a bit overkill. Much easier to write my own sql script that creates a table and then just use the rake db:schema:dump.

If you want to use scaffold following the dump

rails g model ModelName –skip-migration




Posted in RoR | Tagged , , | Leave a comment

Helpful Rails 4 commands

When creating a table and limiting a column string size

rails g scaffold users email:string{50}

To set columns in a table to not nullable or set the default value, after the migration file is created, manually open the file and edit the column you want.

t.string :column1, null:false, default: ‘ABC’

For Join tables via migration

rails g migration CreateJoinTable[Model1Model2] Model1 Model2

The above creates a table with two column model1_id and model2_id.  I’ve experienced that the table name will be Model1_Model2. Rails documentation seems to say different but that’s what I got.

If you want to redo your last migration

rake db:migrate:redo

rake db:migrate:redo STEP=2  # redo last two steps

This was helpful because I had deleted a table rails created and couldn’t figure out how to recreate it until I cam across this

If you created a migration you want to delete

rails db:migrate:status

Find the version before the one you want to delete

rake db:migrate VERSION=XXXXX

Manually delete the migration you don’t want




Posted in Uncategorized | Tagged , | Leave a comment