Created
March 15, 2010 15:04
-
-
Save lukeholder/332918 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
require "simplenoteapi.php"; | |
$notesocket = new simplenoteapi; | |
$notesocket->login('[email protected]', 'pass'); | |
$eachnote = $notesocket->index(); | |
foreach ($eachnote as $note) { | |
echo "<a href=\"index.php?notekey=".$note->key."\">".$note->key. "</a>"; | |
echo "<br/>"; | |
} | |
echo "<br/>"; | |
echo "<br/>"; | |
echo "<br/>"; | |
if(isset($_GET['notekey'])) | |
{ | |
$anote = $notesocket->get_note($_GET["notekey"]); | |
echo "<textarea rows=\"20\" cols=\"100\">".$anote[content]."</textarea>"; | |
echo "<br/>"; | |
echo "<br/>"; | |
print_r($anote[content]); | |
} |
This file contains hidden or 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 | |
class simplenoteapi | |
{ | |
private $email; | |
private $token; | |
private $last_response; | |
private function socket_request ($http_method, $api_method, $data) | |
{ | |
$host = 'simple-note.appspot.com'; | |
$time_out = 10; | |
$response = ''; | |
$errors = array(); | |
if (is_array($data) && count($data)) | |
{ | |
$temp = array(); | |
foreach ($data as $key => $value) | |
{ | |
$temp[] = $key . '=' . $value; | |
} | |
$data = implode('&', $temp); | |
} | |
$headers = array($http_method . ' /api/' . $api_method . ' HTTP/1.0', 'Host: ' . $host); | |
if ($http_method == 'POST') | |
{ | |
$headers[] = 'Content-type: application/x-www-form-urlencoded'; | |
$headers[] = 'Content-length: ' . strlen($data); | |
} | |
$headers[] = 'Connection: Close'; | |
$request = fsockopen('ssl://' . $host, 443, $error['number'], $error['message'], $time_out); | |
if ($request) | |
{ | |
$headers_string = implode("\r\n", $headers) . str_repeat("\r\n", 2) . $data; | |
fwrite($request, $headers_string); | |
stream_set_timeout($request, 10); | |
while (!feof($request)) | |
{ | |
$response .= fgets($request, 1024); | |
} | |
} | |
$return = preg_split("#(\r?\n){2}#", 'Status: ' . $response, 2); | |
$return[0] = preg_split("#\r?\n#", $return[0]); | |
$return_headers = array(); | |
foreach ($return[0] as $header) | |
{ | |
list($key, $value) = explode(': ', $header, 2); | |
$return_headers[$key] = $value; | |
if ($key == 'Status') | |
{ | |
preg_match('#[0-9]{3}#', $value, $http_code); | |
$return['http_code'] = $http_code[0]; | |
} | |
} | |
$return['headers'] = $return_headers; | |
$return['body'] = $return[1]; | |
unset($return[0], $return[1]); | |
return $return; | |
} | |
private function curl_request($url_append, $curl_options = array()) | |
{ | |
$curl_options[CURLOPT_URL] = 'http://simple-note.appspot.com/api/' . $url_append; | |
$curl_options[CURLOPT_HEADER] = true; | |
$curl_options[CURLOPT_RETURNTRANSFER] = true; | |
$ch = curl_init(); | |
curl_setopt_array($ch, $curl_options); | |
$result = curl_exec($ch); | |
$stats = curl_getinfo($ch); | |
$result = explode("\n", $result); | |
$headers = array(); | |
$break = false; | |
unset($result[0]); | |
foreach ($result as $index => $value) | |
{ | |
if (!$break) | |
{ | |
if (trim($value) == '') | |
{ | |
unset($result[$index]); | |
$break = true; | |
} | |
else | |
{ | |
$line = explode(':', $value, 2); | |
$headers[$line[0]] = $line[1]; | |
unset($result[$index]); | |
} | |
} | |
} | |
$result = implode("\n", $result); | |
curl_close($ch); | |
$result = array( | |
'stats' => $stats, | |
'headers' => $headers, | |
'body' => $result | |
); | |
return $result; | |
} | |
/* | |
* Calls the CURL API using a GET | |
*/ | |
private function api_get($method, $parameters = '') | |
{ | |
if (is_array($parameters)) | |
{ | |
foreach ($parameters as $key => $value) | |
{ | |
unset($parameters[$key]); | |
$parameters[] = urlencode($key) . '=' . urlencode($value); | |
} | |
$parameters = implode('&', $parameters); | |
} | |
!empty($parameters) ? $parameters = '?' . $parameters : false ; | |
return $this->curl_request($method . $parameters); | |
} | |
private function api_post ($method, $data) | |
{ | |
return $this->socket_request('POST', $method, $data); | |
} | |
public function login ($email, $password) | |
{ | |
$this->last_response = $this->api_post('login', base64_encode('email=' . urlencode($email) . '&password=' . urlencode($password))); | |
$this->email = $email; | |
$this->token = $this->last_response['body']; | |
} | |
/* | |
* Retrieves the index list | |
* | |
* Returns the data in objects, using the json_decode function | |
* Returns false if it fails for any reason | |
*/ | |
public function index() | |
{ | |
$response = $this->api_get( | |
'index', | |
array( | |
'auth' => $this->token, | |
'email' => $this->email | |
) | |
); | |
if ($response['stats']['http_code'] == 200) | |
{ | |
$response = json_decode($response['body']); | |
return $response; | |
} | |
else | |
{ | |
echo "get failed"; | |
return false; | |
} | |
} | |
public function get_note($note_key) | |
{ | |
$response = $this->api_get( | |
'note', | |
array( | |
'key' => $note_key, | |
'auth' => $this->token, | |
'email' => $this->email, | |
'encode' => 'base64' | |
) | |
); | |
if ($response['stats']['http_code'] == 200) | |
{ | |
$note = array( | |
'key' => $response['headers']['note-key'], | |
'createdate' => $response['headers']['note-createdate'], | |
'modifydate' => $response['headers']['note-modifydate'], | |
'deleted' => (strtolower($response['headers']['note-deleted']) == 'true') ? true : false , | |
'content' => base64_decode($response['body']) | |
); | |
return $note; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/* | |
* Updates or creates a note | |
* | |
* Returns the note key if successful | |
* Returns false if it fails for any reason | |
*/ | |
public function save_note($content, $note_key = '') | |
{ | |
$parameters = array( | |
'auth' => $this->token, | |
'email' => $this->email | |
); | |
if (isset($note_key)) $parameters['key'] = $note_key; | |
$response = $this->api_post( | |
'note', | |
base64_encode($content), | |
$parameters | |
); | |
if ($response['stats']['http_code'] == 200) | |
{ | |
return $response['body']; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/* | |
* Deletes a note | |
* | |
* Returns true if successful | |
* Returns false is it fails for any reason | |
*/ | |
public function delete_note($note_key) | |
{ | |
$response = $this->api_get( | |
'delete', | |
array( | |
'key' => $note_key, | |
'auth' => $this->token, | |
'email' => $this->email | |
) | |
); | |
if ($response['stats']['http_code'] == 200) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/* | |
* API-powered search | |
* | |
* Returns data even for zero results | |
* Returns false if it fails for any reason | |
*/ | |
public function search($search_term, $max_results = 10, $offset_index = 0) | |
{ | |
$response = $this->api_get( | |
'search', | |
array( | |
'query' => urlencode($search_term), | |
'results' => $max_results, | |
'offset' => $offset_index, | |
'auth' => $this->token, | |
'email' => $this->email | |
) | |
); | |
if ($response['stats']['http_code'] == 200) | |
{ | |
$response = json_decode($response['body']); | |
$return = array( | |
'count' => $response->Response->totalRecords, | |
'results' => array() | |
); | |
if ($return['count'] > 0) | |
{ | |
foreach ($response->Response->Results as $result) | |
{ | |
if (!empty($result->key)) $return['results'][$result->key] = $result->content; | |
} | |
} | |
return $return; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment