Skip to content

Instantly share code, notes, and snippets.

@abrahamvegh
Created January 8, 2010 04:45
Show Gist options
  • Save abrahamvegh/271858 to your computer and use it in GitHub Desktop.
Save abrahamvegh/271858 to your computer and use it in GitHub Desktop.
<?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 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'];
}
}
$api = new simplenoteapi;
$api->login('email@domain.tld', 'password');
print_r($api);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment