Please refer to the Google Maps API Geocoder: http://lab.abhinayrathore.com/ipmapper/
That will answer all of your questions from A-Z. If you are having trouble putting it to use check out where this question has been asked and answered: Find Latitude & Longitude of a given IP Address using IPMapper
This will be done using Javascript and not PHP