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

Commit

Permalink
adding search
Browse files Browse the repository at this point in the history
  • Loading branch information
qzio committed Dec 16, 2010
1 parent fcba2e7 commit a8ca499
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
61 changes: 61 additions & 0 deletions example/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

session_start();

// Our configuration, edit it to set your API credentials
require_once('config.php');

// OAuth library
require_once('oauth.php');

// Flattr REST library
require_once('flattr_rest.php');

function loadToken()
{
return array($_SESSION['flattr_access_token'], $_SESSION['flattr_access_token_secret']);
}

// Load token
list($token, $token_secret) = loadToken();

// Setup a new client
$flattr = new Flattr_Rest(APP_KEY, APP_SECRET, $token, $token_secret);

// Did we succeed?
if ( $flattr->error() )
{
die( 'Error ' . $flattr->error() );
}

// Get the thing list, using a search query
if (isset($_GET['q']))
{
$things = $flattr->getSearchThingList($_GET['q']);
}
else
{
$things = array();
echo 'ok didnt search for anything, listnening your things insteed';
$things = $flattr->getThingList();
}

?>
<p>
found <?php echo count($things) ?> things.
</p>
<?php foreach ( $things as $thing ): ?>
<p>
Title: <?php echo $thing['title'] ?>
</p>
<?php endforeach ?>

<h2> Search for things</h2>
<form method="get" action="search.php">
<div>
<input type="text" name="q" value="the search query">
</div>
<div>
<input type="submit">
</div>
</form>
22 changes: 22 additions & 0 deletions flattr_rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ public function getThingList($userid = null)
return $things;
}

public function getSearchThingList($query)
{
$result = $this->get($this->actionUrl('/thing/search/q/' . urlencode($query)) );
if (empty($result))
{
return array();
}
$dom = new DOMDocument();
$dom->loadXml($result);
$thingXml = $dom->getElementsByTagName('thing');
$things = array();
foreach ($thingXml as $thing)
{
$thingdata = $this->parseThingXml($thing);
if ( is_array($thingdata) )
{
$things[] = $thingdata;
}
}
return $things;
}

public function getLanguages()
{
$result = $this->get($this->actionUrl('/feed/languages'));
Expand Down

0 comments on commit a8ca499

Please sign in to comment.