crcx / pahaki

PHP code for accessing the Xtify API and Geocoding APIs

This URL has Read+Write access

pahaki / pahaki.php
100644 123 lines (97 sloc) 3.09 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*---------------------------------------------------------
Pahaki
Geocoding and Location Awareness API
-------------------------------------------------------*/
 
class pahaki
{
  public $lat; /* Latitude */
  public $lon; /* Longitude */
  public $coords; /* String. Latitude,Longitude */
  public $city; /* City Name */
  public $county; /* County Name */
  public $state; /* Two Letter State Name */
  public $zip; /* Zip Code */
  public $street1; /* Street #1 (Intersection) */
  public $street2; /* Street #2 (Intersection) */
 
  public function __construct($uuid)
  {
    /* Query Xtify for basic position */
    $cpid = "";
    $xml = download('http://query.xtify.com/api/1.0/xml/location?userkey='.$uuid.'&cpid='.$cpid);
    $x = new SimpleXMLElement(utf8_decode($xml));
 
    $this->lat = $x->locationset->location->coords->lat;
    $this->lon = $x->locationset->location->coords->lon;
    $this->coords = $lat.','.$lon;
 
    /* Reverse Geocoding from geonames.org */
    $geo = download('http://ws.geonames.org/findNearestIntersection?lat='.$lat.'&lng='.$lon);
    $x = new SimpleXMLElement(utf8_decode($geo));
    $this->state = $x->intersection->adminCode1;
    $this->county = $x->intersection->adminName2;
  }
 
  private function download($url)
  {
    $crl = curl_init();
    $timeout = 8;
    curl_setopt ($crl, CURLOPT_URL,$url);
    curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
    $ret = curl_exec($crl);
    curl_close($crl);
    return $ret;
  }
}
 
 
/* Your Xtify CPID */
$cpid = "";
 
 
function pahaki_get($url)
{
  $crl = curl_init();
  $timeout = 8;
  curl_setopt ($crl, CURLOPT_URL,$url);
  curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
  $ret = curl_exec($crl);
  curl_close($crl);
  return $ret;
}
 
 
 
function pahaki_locate($uuid)
{
  $xml = pahaki_get('http://query.xtify.com/api/1.0/xml/location?userkey='.$uuid.'&cpid='.$cpid);
  return $xml;
}
 
 
function pahaki_lat($xml)
{
  $x = new SimpleXMLElement(utf8_decode($xml));
  return $x->locationset->location->coords->lat;
}
 
 
function pahaki_lon($xml)
{
  $x = new SimpleXMLElement(utf8_decode($xml));
  return $x->locationset->location->coords->lon;
}
 
 
function pahaki_timestamp($xml)
{
  $x = new SimpleXMLElement(utf8_decode($xml));
  return $x->locationset->location->timestamp;
}
 
 
function pahaki_getCoords($xml)
{
  return pahaki_lat($xml).','.pahaki_lon($xml);
}
 
 
function pahaki_getState($xml)
{
  $lat = pahaki_lat($xml);
  $lon = pahaki_lon($xml);
  $geo = pahaki_get('http://ws.geonames.org/findNearestIntersection?lat='.$lat.'&lng='.$lon);
  $x = new SimpleXMLElement(utf8_decode($geo));
  return $x->intersection->adminCode1;
}
 
 
function pahaki_getCounty($xml)
{
  $lat = pahaki_lat($xml);
  $lon = pahaki_lon($xml);
  $geo = pahaki_get('http://ws.geonames.org/findNearestIntersection?lat='.$lat.'&lng='.$lon);
  $x = new SimpleXMLElement(utf8_decode($geo));
  return $x->intersection->adminName2;
}
 
?>