{"id":729,"date":"2011-02-10T12:34:14","date_gmt":"2011-02-10T17:34:14","guid":{"rendered":"http:\/\/abrammorphew.com\/notes\/?p=729"},"modified":"2024-10-28T15:35:28","modified_gmt":"2024-10-28T20:35:28","slug":"php-geocoding-with-curl-and-json","status":"publish","type":"post","link":"http:\/\/abrammorphew.com\/notes\/2011\/02\/10\/php-geocoding-with-curl-and-json\/","title":{"rendered":"PHP: Geocoding with cURL and JSON&#8230;"},"content":{"rendered":"\n<p>wanting to balance the work load for a site between server-side and client-side tasks, i abstracted this very simple geocoding class with Google Maps.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:false,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;darcula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">\/*\n##############################################################\nVersion: 1.0\nAuthor: Abram Morphew\nCreated: 2011-02-10\n\nFile: class.Geocoder.php\nDesc: PHP class for geocoding with Google Geocoder API. No API\nkey is necessary to use this class.\n\n##############################################################\n*\/\nif(!defined('APPNAME')) { die('Hacking attempt'); }\n\nclass Geocoder {\n    public $status = null;\n    public $address = null;\n    public $response = array();\n    public $coordinates = array();\n\n    function __construct() { }\n\n    function coordinates($address=null,$city=null,$state=null,$postcode=null,$country=null) {\n        $query = urlencode($address.&quot;,&quot;.$city.&quot;,&quot;.$state.&quot;,&quot;.$postcode.&quot;,&quot;.$country);\n        $url = &quot;http:\/\/maps.google.com\/maps\/api\/geocode\/json?address=&quot;.$query.&quot;&amp;sensor=false&quot;; \n        $ch = curl_init();\n        curl_setopt($ch, CURLOPT_URL, $url);\n        curl_setopt($ch, CURLOPT_HEADER, false);\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n        $response = curl_exec($ch);\n        curl_close($ch);\n\n        \/\/ set public vars and return... \n        $this-&gt;response = json_decode($response,true);\n        $this-&gt;status = $this-&gt;response['status'];\n        $this-&gt;address = $this-&gt;response['results'][0]['formatted_address'];\n        $this-&gt;coordinates = $this-&gt;response['results'][0]['geometry']['location'];\n        return $this-&gt;coordinates;\n    }\n\n}<\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\"><br><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>wanting to balance the work load for a site between server-side and client-side tasks, i abstracted this very simple geocoding class with Google Maps.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-notes"],"_links":{"self":[{"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":4,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":1608,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/posts\/729\/revisions\/1608"}],"wp:attachment":[{"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/abrammorphew.com\/notes\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}