Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
minor tweaks; added sample for "flattr" resource
Browse files Browse the repository at this point in the history
  • Loading branch information
qzio committed Jan 27, 2012
1 parent 284abc4 commit 3f0d2b8
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 24 deletions.
22 changes: 18 additions & 4 deletions README.md
Expand Up @@ -8,13 +8,27 @@ if you are using a server with apt you can install it by


apt-get install php5-curl 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 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)
13 changes: 10 additions & 3 deletions bootstrap.php
Expand Up @@ -7,9 +7,6 @@
require_once __DIR__ . '/lib/configbase.php'; require_once __DIR__ . '/lib/configbase.php';
require_once __DIR__ . '/config.php'; require_once __DIR__ . '/config.php';
} }
session_start();
header('Content-type: text/html; charset=utf-8');

// simple log wraps error_log, // simple log wraps error_log,
function slog($message) function slog($message)
{ {
Expand All @@ -27,3 +24,13 @@ function slog($message)
error_log($message."\n", 3, ConfigFlattr::$LOGFILE); 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');
}
49 changes: 40 additions & 9 deletions lib/oauth2client.php
Expand Up @@ -6,7 +6,10 @@ class OAuth2Client
protected $http = null; 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() ) public function __construct( $settings = array() )
{ {
Expand Down Expand Up @@ -34,6 +37,8 @@ public function __construct( $settings = array() )
} }


/** /**
* assemble the authorize_url link
*
* @return string authorize url endpoint * @return string authorize url endpoint
*/ */
public function authorizeUrl() public function authorizeUrl()
Expand All @@ -46,6 +51,8 @@ public function authorizeUrl()
} }


/** /**
* exchange $code for an access_token
*
* @param string $code * @param string $code
* @throws exception * @throws exception
* @return mixed string or false * @return mixed string or false
Expand Down Expand Up @@ -99,6 +106,8 @@ public function fetchAccessToken($code, $authInParams = false)
} }


/** /**
* perform a GET api call
*
* @param string $path * @param string $path
* @return HttpResponse * @return HttpResponse
*/ */
Expand All @@ -121,6 +130,8 @@ public function get($path)
} }


/** /**
* perform a GET api call, wrapped in OAuth2Client::parseResponse
*
* @param string $path * @param string $path
* @return mixed array or string * @return mixed array or string
*/ */
Expand All @@ -137,13 +148,14 @@ public function getParsed($path)
} }


/** /**
* perform a POST api call
*
* @param string $path * @param string $path
* @param array $postarray * @param array $postarray
* @return HttpResponse * @return HttpResponse
*/ */
public function post($path, $postarray) public function post($path, $postarray)
{ {
slog("doing POST...");
$this->logRequest( $this->logRequest(
'POST '.$this->uriFor($path), 'POST '.$this->uriFor($path),
array( array(
Expand All @@ -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 * @return array $headers
*/ */
protected function headers($authorizationHeader = true) protected function headers($authorizationHeader = true)
Expand All @@ -176,7 +192,8 @@ protected function headers($authorizationHeader = true)
if (! empty($this->settings['access_token'])) { if (! empty($this->settings['access_token'])) {
$headers['Authorization'] = $this->settings['token_param_name'] . ' ' $headers['Authorization'] = $this->settings['token_param_name'] . ' '
. $this->settings['access_token']; . $this->settings['access_token'];
} else { } else if (! empty($this->settings['client_id']) &&
! empty($this->settings['client_secret'])) {
$headers['Authorization'] = 'Basic ' . base64_encode( $headers['Authorization'] = 'Basic ' . base64_encode(
$this->settings['client_id'] . ':' . $this->settings['client_secret'] $this->settings['client_id'] . ':' . $this->settings['client_secret']
); );
Expand All @@ -188,6 +205,8 @@ protected function headers($authorizationHeader = true)
} }
/** /**
* @todo something to parse yaml, xml, m.m ? * @todo something to parse yaml, xml, m.m ?
* parses a response and returns an array if successfull
*
* @param HttpResponse $response * @param HttpResponse $response
* @return mixed array or string $response->body * @return mixed array or string $response->body
*/ */
Expand All @@ -203,6 +222,8 @@ public static function parseResponse($response)
} }


/** /**
* adds the base_url before a resource $path
*
* @param string $path * @param string $path
* @return string * @return string
*/ */
Expand All @@ -212,10 +233,15 @@ protected function uriFor($path)
} }




/**
* log a response
*
* @param HttpResponse
* @return void
*/
protected function logResponse($response) protected function logResponse($response)
{ {
if ($this->settings['developer_mode']) if ($this->settings['developer_mode'] && function_exists('slog')) {
{
slog(""); slog("");
slog("** LOG RESPONSE **"); slog("** LOG RESPONSE **");
slog("HEADERS =>"); slog("HEADERS =>");
Expand All @@ -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("");
slog("** LOG REQUEST **"); slog("** LOG REQUEST **");
slog($uri); slog($uri);
slog($prms); slog($params);
} }
} }
} }
17 changes: 10 additions & 7 deletions public/authenticated_calls.php
Expand Up @@ -3,23 +3,26 @@


if ( empty($_SESSION['access_token']) ) { if ( empty($_SESSION['access_token']) ) {
header('index.php'); header('index.php');
exit;
} }
$client = new OAuth2Client( ConfigFlattr::all(array('access_token' => $_SESSION['access_token'])) ); $client = new OAuth2Client( ConfigFlattr::all(array('access_token' => $_SESSION['access_token'])) );

// getParsed is just a get() wrapped in oauth2client::parseResponse()
$profile = $client->getParsed('/user'); $profile = $client->getParsed('/user');
$flattrs = $client->getParsed('/user/flattrs'); $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="/index.php?logout=true">logout</a>
<a href="/submit.php">submit new thing</a> <a href="/submit.php">submit new thing</a>
<h1>Authenticated as as</h1> <a href="/flattr.php">flattr something</a>
<pre><?php var_dump($profile)?></pre> <h1>Authenticated as as <?php echo h($profile['username'])?></h1>


<hr/> <hr/>
flatterings: flatterings: <?php echo count($flattrs)?>
<pre><?php var_dump($flattrs)?></pre> <pre><?php var_dump($flattrs)?></pre>
<hr/> <hr/>
things: things: <?php echo count($things)?>
<pre><?php var_dump($things)?></pre> <pre><?php var_dump($things)?></pre>


43 changes: 43 additions & 0 deletions 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 } ?>
2 changes: 1 addition & 1 deletion public/unauthenticated_calls.php
Expand Up @@ -16,7 +16,7 @@


/thing/187509/flattr-on-Flattr: /thing/187509/flattr-on-Flattr:
<pre><?php var_dump($thing);?></pre> <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> <pre><?php var_dump($user);?></pre>
<hr/> <hr/>
a /lookup of http://blog.flattr.net/2011/10/api-v2-beta-out-whats-changed/ returned a /lookup of http://blog.flattr.net/2011/10/api-v2-beta-out-whats-changed/ returned
Expand Down

0 comments on commit 3f0d2b8

Please sign in to comment.