Solved

PHP Geocoding question

Posted on 2014-10-26
3
190 Views
Last Modified: 2014-10-26
I'm trying to get lat and long when geocoding an address.

For example this address as $address:

http://www.geocodefarm.com/api/forward/json/c3140ba389a289bd42c51ebe2d72ccc67fb4633c/1058 W Club Blvd,Durham,North-Carolina,27701

Open in new window


This is the code:

 
// Take the address in a variable
	$address = $attorneysStuff['bizAddr'].','.$attorneysStuff['bizCity'].','.$attorneysStuff['bizState'].','.$attorneysStuff['bizZip'];
 
 
$address_url = "http://www.geocodefarm.com/api/forward/json/c3140ba389a289bd42c51ebe2d72ccc67fb4633c/$address";



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $address_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
 
$response_a = json_decode($response);
 
//Print the provided address in Human readable/ complete Postal address;
echo $address_returned = $response_a->results[0]->ADDRESS->address_returned.'<br />';
 
//Print the Latitude and Longitude of the address
echo $lat = $response_a->geocoding_results[0]->COORDINATES->latitude.'<br />';
echo $long = $response_a->geocoding_results[0]->COORDINATES->longitude;

Open in new window



The problem is echo $address_returned  and echo $lat and echo $long won't print anything: I'm sure there is an error in the print string not in the curl but I can't find it.
0
Comment
Question by:seopti
  • 2
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40405417
Post the json response.
0
 

Author Comment

by:seopti
ID: 40405419
When you copy and paste the first string in your browser you can see the json response which is:

{

    "geocoding_results": {
        "LEGAL_COPYRIGHT": {
            "copyright_notice": "Copyright (c) 2013 Grabenhofen Corporation (Colorado, USA) - All Rights Reserved.",
            "result_distribution": "UNPERMITTED, UNLICENSED",
            "copyright_logo": "https://www.geocodefarm.com/assets/img/logo.png",
            "terms_of_service": "https://www.geocodefarm.com/policies/terms-of-service/",
            "privacy_policy": "https://www.geocodefarm.com/policies/privacy-policy/"
        },
        "STATUS": {
            "access": "KEY_VALID, ACCESS_GRANTED",
            "status": "SUCCESS"
        },
        "ACCOUNT": {
            "name": "Thomas Kowalewski",
            "email": "support@apollofind.com",
            "api_key": "c3140ba389a289bd42c51ebe2d72ccc67fb4633c",
            "monthly_due": "75.00",
            "next_due": "2014-11-25 23:06:13",
            "distribution_license": "NONE, UNLICENSED",
            "usage_limit": "100000",
            "used_today": "239",
            "remaining_queries": "99761"
        },
        "PROVIDER": {
            "provider": "LOCAL_FARM",
            "import": "ALREADY_STORED"
        },
        "ADDRESS": {
            "address_provided": "1058 W Club Blvd,Durham,North-Carolina,27701",
            "address_returned": "Northgate Mall, 1058 West Club Boulevard, Durham, NC 27701, USA",
            "accuracy": "VERY ACCURATE"
        },
        "COORDINATES": {
            "latitude": "36.0201168129003",
            "longitude": "-78.9100018570646"
        },
        "STATISTICS": {
            "load_time": "0.38",
            "https_ssl": "DISABLED, INSECURE"

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40405433
$response_a = json_decode($response);

//Print the provided address in Human readable/ complete Postal address;
echo $address_returned = $response_a->geocoding_results->ADDRESS->address_returned.'<br />';
 
//Print the Latitude and Longitude of the address
echo $lat = $response_a->geocoding_results->COORDINATES->latitude.'<br />';
echo $long = $response_a->geocoding_results->COORDINATES->longitude;

Open in new window

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TCPDF - Create PDF from Form Values and Link to PDF Download 7 45
Force PDF to open inline as opposed to dowload 16 35
PHP 5.6 and 7.x 4 37
PHP: Filling Out/Creating a PDF 29 94
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question