Skip to content
Browse files

minor tweaks; added sample for "flattr" resource

  • Loading branch information...
1 parent 284abc4 commit 3f0d2b8a9aac55548a132c86fde7048566377069 @qzio qzio committed
Showing with 122 additions and 24 deletions.
  1. +18 −4 README.md
  2. +10 −3 bootstrap.php
  3. +40 −9 lib/oauth2client.php
  4. +10 −7 public/authenticated_calls.php
  5. +43 −0 public/flattr.php
  6. +1 −1 public/unauthenticated_calls.php
View
22 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)
View
13 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');
+}
View
49 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);
}
}
}
View
17 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
?>
<a href="/index.php?logout=true">logout</a>
<a href="/submit.php">submit new thing</a>
-<h1>Authenticated as as</h1>
-<pre><?php var_dump($profile)?></pre>
+<a href="/flattr.php">flattr something</a>
+<h1>Authenticated as as <?php echo h($profile['username'])?></h1>
<hr/>
-flatterings:
+flatterings: <?php echo count($flattrs)?>
<pre><?php var_dump($flattrs)?></pre>
<hr/>
-things:
+things: <?php echo count($things)?>
<pre><?php var_dump($things)?></pre>
-
-
View
43 public/flattr.php
@@ -0,0 +1,43 @@
+<?php
+require_once __DIR__ . '/../bootstrap.php';
+
+if ( empty($_SESSION['access_token']) ) {
+ header('index.php');
+ exit;
+}
+
+$thingIdList= array('466757','465172','467437');
+
+$client = new OAuth2Client( ConfigFlattr::all(array('access_token' => $_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);
+}
+?>
+<a href="/index.php?logout=true">logout</a>
+<a href="/authenticated_calls.php">profile</a>
+<a href="/submit.php">submit new thing</a>
+<h1>Authenticated as as <?php echo h($profile['username'])?> </h1>
+<hr/>
+
+<?php foreach($things as $thing) { ?>
+ <div>
+ <?php if ($thing['flattred']) { ?>
+ <?php echo h($thing['url'])?> is already flattred by the authenticated user
+ <?php } else { ?>
+ <a href="/flattr.php?thing_url=<?php echo h($thing['url'])?>">flattr <?php echo h($thing['title'])?></a>
+ <?php } ?>
+ </div>
+<?php } ?>
View
2 public/unauthenticated_calls.php
@@ -16,7 +16,7 @@
/thing/187509/flattr-on-Flattr:
<pre><?php var_dump($thing);?></pre>
-<hr/>our(flattr) profile on flattr.com:
+<hr/>the user "flattr" on flattr.com:
<pre><?php var_dump($user);?></pre>
<hr/>
a /lookup of http://blog.flattr.net/2011/10/api-v2-beta-out-whats-changed/ returned

0 comments on commit 3f0d2b8

Please sign in to comment.
Something went wrong with that request. Please try again.