From 3f0d2b8a9aac55548a132c86fde7048566377069 Mon Sep 17 00:00:00 2001 From: Joel Hansson Date: Fri, 27 Jan 2012 18:15:03 +0100 Subject: [PATCH] minor tweaks; added sample for "flattr" resource --- README.md | 22 +++++++++++--- bootstrap.php | 13 +++++++-- lib/oauth2client.php | 49 ++++++++++++++++++++++++++------ public/authenticated_calls.php | 17 ++++++----- public/flattr.php | 43 ++++++++++++++++++++++++++++ public/unauthenticated_calls.php | 2 +- 6 files changed, 122 insertions(+), 24 deletions(-) create mode 100644 public/flattr.php diff --git a/README.md b/README.md index 850a795..05693a9 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,27 @@ if you are using a server with apt you can install it by apt-get install php5-curl -point a vhost to the public/ directory. +## public api call + + $client = new OAuth2Client(array('base_url' => 'https://api.flattr.com/rest/v2')); + $thing = $client->getParsed('/things/423405/'); + var_dump($thing); // array + +## minimal sample code create an API key at https://flattr.com/apps +make sure you add a correct callback\_url it should be something like http://url-to-your-vhost/callback.php +Copy /config.template.php to /config.php and enter your api credentials. +Point a apache/lighttpd/nginx vhost to the minimal/ directory and restart. +Open http://url-to-your-vhost + +## connect with flattr sample -copy /config.template.php to /config.php and enter your api credentials +sample app that implements a connect with flattr (based on sessions, no database, using coltrane framework) -visit http://localhost or whatever domain you assigned to your vhost. +see connect\_with\_flattr/ ---- -documentation at [http://developers.flattr.net/v2](http://developers.flattr.net/v2) +Documentation at [http://developers.flattr.net/](http://developers.flattr.net/) +Questions: [StackOverflow](http://stackoverflow.com/questions/tagged/flattr) +Feedback: [twitter](https://twitter.com/#!/flattr) diff --git a/bootstrap.php b/bootstrap.php index fccbf11..83edf33 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -7,9 +7,6 @@ require_once __DIR__ . '/lib/configbase.php'; require_once __DIR__ . '/config.php'; } -session_start(); -header('Content-type: text/html; charset=utf-8'); - // simple log wraps error_log, function slog($message) { @@ -27,3 +24,13 @@ function slog($message) error_log($message."\n", 3, ConfigFlattr::$LOGFILE); } } + +function h($str) +{ + return htmlentities($str,ENT_QUOTES, "UTF-8"); +} + +if (php_sapi_name() != 'cgi' && !empty($_SERVER['REMOTE_ADDR'])) { + session_start(); + header('Content-type: text/html; charset=utf-8'); +} diff --git a/lib/oauth2client.php b/lib/oauth2client.php index 875ec9c..be74023 100644 --- a/lib/oauth2client.php +++ b/lib/oauth2client.php @@ -6,7 +6,10 @@ class OAuth2Client protected $http = null; /** - * @param array $settings + * creates the oauth2client object with some defaults + * + * @param array $settings overrides default settings. + * @return OAuth2Client */ public function __construct( $settings = array() ) { @@ -34,6 +37,8 @@ public function __construct( $settings = array() ) } /** + * assemble the authorize_url link + * * @return string authorize url endpoint */ public function authorizeUrl() @@ -46,6 +51,8 @@ public function authorizeUrl() } /** + * exchange $code for an access_token + * * @param string $code * @throws exception * @return mixed string or false @@ -99,6 +106,8 @@ public function fetchAccessToken($code, $authInParams = false) } /** + * perform a GET api call + * * @param string $path * @return HttpResponse */ @@ -121,6 +130,8 @@ public function get($path) } /** + * perform a GET api call, wrapped in OAuth2Client::parseResponse + * * @param string $path * @return mixed array or string */ @@ -137,13 +148,14 @@ public function getParsed($path) } /** + * perform a POST api call + * * @param string $path * @param array $postarray * @return HttpResponse */ public function post($path, $postarray) { - slog("doing POST..."); $this->logRequest( 'POST '.$this->uriFor($path), array( @@ -165,6 +177,10 @@ public function post($path, $postarray) } /** + * assemble headers for an API clall, + * adding Accept and potentially Authorization + * + * @param boolean $authorizationHeader * @return array $headers */ protected function headers($authorizationHeader = true) @@ -176,7 +192,8 @@ protected function headers($authorizationHeader = true) if (! empty($this->settings['access_token'])) { $headers['Authorization'] = $this->settings['token_param_name'] . ' ' . $this->settings['access_token']; - } else { + } else if (! empty($this->settings['client_id']) && + ! empty($this->settings['client_secret'])) { $headers['Authorization'] = 'Basic ' . base64_encode( $this->settings['client_id'] . ':' . $this->settings['client_secret'] ); @@ -188,6 +205,8 @@ protected function headers($authorizationHeader = true) } /** * @todo something to parse yaml, xml, m.m ? + * parses a response and returns an array if successfull + * * @param HttpResponse $response * @return mixed array or string $response->body */ @@ -203,6 +222,8 @@ public static function parseResponse($response) } /** + * adds the base_url before a resource $path + * * @param string $path * @return string */ @@ -212,10 +233,15 @@ protected function uriFor($path) } + /** + * log a response + * + * @param HttpResponse + * @return void + */ protected function logResponse($response) { - if ($this->settings['developer_mode']) - { + if ($this->settings['developer_mode'] && function_exists('slog')) { slog(""); slog("** LOG RESPONSE **"); slog("HEADERS =>"); @@ -225,14 +251,19 @@ protected function logResponse($response) } } - protected function logRequest( $uri, $prms = array()) + /** + * log a request + * + * @param string $uri + * @param array $params + */ + protected function logRequest( $uri, $params = array()) { - if ($this->settings['developer_mode']) - { + if ($this->settings['developer_mode'] && function_exists('slog')) { slog(""); slog("** LOG REQUEST **"); slog($uri); - slog($prms); + slog($params); } } } diff --git a/public/authenticated_calls.php b/public/authenticated_calls.php index 4d952a9..16d6378 100644 --- a/public/authenticated_calls.php +++ b/public/authenticated_calls.php @@ -3,23 +3,26 @@ if ( empty($_SESSION['access_token']) ) { header('index.php'); + exit; } $client = new OAuth2Client( ConfigFlattr::all(array('access_token' => $_SESSION['access_token'])) ); + +// getParsed is just a get() wrapped in oauth2client::parseResponse() $profile = $client->getParsed('/user'); $flattrs = $client->getParsed('/user/flattrs'); -$things = $client->getParsed('/user/things'); +$things = $client->getParsed('/user/things'); + +// proper html is intentially left out to minimize code ?> logout submit new thing -

Authenticated as as

-
+flattr something +

Authenticated as as


-flatterings: +flatterings:

-things: +things:
- - diff --git a/public/flattr.php b/public/flattr.php new file mode 100644 index 0000000..e3aec4d --- /dev/null +++ b/public/flattr.php @@ -0,0 +1,43 @@ + $_SESSION['access_token'])) ); + +if (!empty($_GET['thing_url'])) +{ + $thingUrl = $_GET['thing_url']; + echo "will try to flattr ".$thingUrl; + $response = Oauth2Client::parseResponse($client->post('/flattr', array('url' => $thingUrl))); + var_dump($response); + exit; +} + +$profile = $client->getParsed('/user'); + +$things = array(); +foreach($thingIdList as $thingId) { + $things[] = $client->getParsed('/things/'.$thingId); +} +?> +logout +profile +submit new thing +

Authenticated as as

+
+ + +
+ + is already flattred by the authenticated user + + flattr + +
+ diff --git a/public/unauthenticated_calls.php b/public/unauthenticated_calls.php index 602f3de..3f2a29e 100644 --- a/public/unauthenticated_calls.php +++ b/public/unauthenticated_calls.php @@ -16,7 +16,7 @@ /thing/187509/flattr-on-Flattr:
-
our(flattr) profile on flattr.com: +
the user "flattr" on flattr.com:

a /lookup of http://blog.flattr.net/2011/10/api-v2-beta-out-whats-changed/ returned