Created
June 30, 2009 04:51
-
-
Save barkerja/138000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Brightkite PHP Object to do basic things such as checkin, post a note, and search | |
* for location. | |
* | |
* This object is chain-able. | |
* Ex: | |
* Brightkite::factory()->current_location()->note('new note'); | |
* Brightkite::factory()->address('San Francisco, CA')->checkin()->note('new note'); | |
* Brightkite::factory()->address('@placemark')->checkin(); | |
* | |
* @author John Barker <jabarker1@gmail.com> | |
* @copyright (c) 2009 John Barker | |
*/ | |
class Brightkite { | |
protected $location; | |
protected $last_method; | |
private $username = ''; | |
private $password = ''; | |
/** | |
* Factory method to allow for chaining | |
* | |
* @chainable | |
* @return object | |
*/ | |
public static function factory() | |
{ | |
return new Brightkite; | |
} | |
/** | |
* Sets users current checked in location. | |
* | |
* @chainable | |
*/ | |
public function current_location() | |
{ | |
$data = $this->call('http://brightkite.com/me'); | |
$this->location = 'http://brightkite.com/places/'.$data->place->id; | |
$this->last_method = __FUNCTION__; | |
return $this; | |
} | |
/** | |
* Searches for address and uses first match. | |
* | |
* @chainable | |
* @param string location in form of address, city, etc. | |
* @return object | |
*/ | |
public function address($loc) | |
{ | |
$this->last_method = __FUNCTION__; | |
$data = $this->call('http://brightkite.com/places/search.xml?q='.urlencode($loc)); | |
if (count($data->place) > 1) | |
{ | |
$this->location = $data->place[0]->id; | |
} | |
else | |
{ | |
$this->location = 'http://brightkite.com/places/'.$data->id; | |
} | |
return $this; | |
} | |
/** | |
* Checks you in with result from address() | |
* | |
* @chainable | |
* @return object | |
*/ | |
public function checkin() | |
{ | |
if ($this->last_method == 'current_location') | |
{ | |
throw new Exception('You are attempting to check in to a place you are already checked into.'); | |
} | |
$this->last_method = __FUNCTION__; | |
$this->call($this->location.'/checkins'); | |
return $this; | |
} | |
/** | |
* Post a note with result from either address() | |
* or current_location(). | |
* | |
* @param string Note to be sent. | |
*/ | |
public function note($note) | |
{ | |
if (empty($note)) | |
{ | |
throw new Exception('You can not post an empty note.'); | |
} | |
$this->last_method = __FUNCTION__; | |
$this->call($this->location.'/notes', array('note[body]' => $note)); | |
} | |
/** | |
* Creates and makes the web service call to Brightkite. | |
* | |
* @param string URL to call API | |
* @param array params to be passed to WS call | |
* @return object | |
*/ | |
public function call($url = NULL, $params = NULL) | |
{ | |
$url = ( ! is_null($url)) ? $url : $this->location; | |
if ($this->last_method != 'address') | |
{ | |
$url = $url.'.xml'; | |
} | |
$ch = curl_init(); | |
if (empty($this->username) OR empty($this->password)) | |
{ | |
throw new Exception('You must set the username and password properties.'); | |
} | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
if (in_array($this->last_method, array('note', 'checkin'))) | |
{ | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
} | |
if ( ! $data = curl_exec($ch)) | |
{ | |
throw new Exception('Error making WS call: '.curl_error($ch)); | |
} | |
return simplexml_load_string($data); | |
} | |
/** | |
* Return current location | |
* | |
* @return string | |
*/ | |
public function get_location() | |
{ | |
return $this->location; | |
} | |
} | |
try { | |
echo Brightkite::factory()->current_location()->note('I can haz n0tez!'); | |
} | |
catch (Exception $e) | |
{ | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment