Source for file Technorati.php
Documentation is available at Technorati.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: James Stewart <james@jystewart.net> |
// +----------------------------------------------------------------------+
// $id: Technorati.php,v 0.5.5 2005/04/16 16:49:00 jystewart Exp $
* using PEAR error management
* using XML_Serializer for processing
require_once 'XML/Unserializer.php';
* uses HTTP to send requests
require_once 'HTTP/Request.php';
* uses Cache_Lite for caching
require_once 'Cache/Lite.php';
* Client for Technorati's REST-based webservices
* Technorati is a blog search engine with a number of tools to help
* explore and utilise the blogosphere. The API provides enhanced
* access to all the site's features
* @author James Sytewart <james@jystewart.net>
* @package Services_Technorati
* @todo update once attention.xml query is stabilised
* @link http://pear.php.net/package/Services_Technorati
var $_apiUrl = 'http://api.technorati.com';
* XML_Unserializer, used to parse the XML
* @var object XML_Unserializer
* @param int hoursToCache
* @param string pathToCache
$this->_apiKey = $apiKey;
if (! empty ($hoursToCache)) {
$this->_hoursToCache = $hoursToCache;
$this->_pathToCache = $pathToCache;
'cacheDir' => $pathToCache,
'lifeTime' => (3600 * $hoursToCache)
$this->_cache = new Cache_Lite ($cache_options);
* Factory methods to create client
* @param int hoursToCache
* @param string pathToCache
* @return Services_Technorati object
function factory($apiKey, $hoursToCache = '', $pathToCache = '')
* KeyInfo provides information on daily usage of an API key.
* A Technorati API key is typically limited to 500 requests
* per day, where a day is measured as 00:00-23:59 PST
* This function does not cache, and this call does not use any
* of the daily request allocation for the given key.
return $this->_sendRequest ('keyinfo');
* Cosmos lets you see what blogs are linking to a given URL
function cosmos($url, $options)
/* Check for invalid options */
$valid_options = array ('type','limit','start','current','claim','highlight');
$options = $this->_checkOptions ($options, $valid_options);
if (PEAR ::isError ($options)) {
$filename = $url. "cosmos". join("-",$options);
if (!empty ($this->_cache) && $cache = $this->_cache->get ($filename)) {
$value = $this->_sendRequest ('cosmos',$options);
if (! PEAR ::isError ($value) && !empty ($this->_cache)) {
$this->_cache->save ($value);
* The search lets you see what blogs contain a given search string
function search($query, $options = array ())
/* Check for invalid options */
$valid_options = array ('start','limit','claim');
$options = $this->_checkOptions ($options, $valid_options);
if (PEAR ::isError ($options)) {
$options = array ('query'=> urlencode($query));
$filename = "search.". join("_",$query). join("_",$options);
if ($cache = $this->_cache->get ($filename)) {
$value = $this->_sendRequest ('search',$options);
if (! PEAR ::isError ($value) && !empty ($this->_cache)) {
$this->_cache->save ($value);
* The getinfo query tells you things that Technorati knows about a user
$options = array ('username'=> urlencode($username));
$filename = " getinfo.$username";
if ($cache = $this->_cache->get ($filename)) {
$value = $this->_sendRequest ('getinfo',$options);
if (! PEAR ::isError ($value) && !empty ($this->_cache)) {
$this->_cache->save ($value);
* The outbound query lets you see what blogs are linked to from a given
* blog, including their associated info.
function outbound($url, $options = array ())
/* Check for invalid options */
$valid_options = array ('start');
$options = $this->_checkOptions ($options, $valid_options);
if (PEAR ::isError ($options)) {
$filename = $url. "outbound.". join("_",$options);
if ($cache = $this->_cache->get ($filename)) {
$value = $this->_sendRequest ('outbound',$options);
if (! PEAR ::isError ($value) && !empty ($this->_cache)) {
$this->_cache->save ($value);
* The bloginfo query provides info on what blog, if any, is
* associated with a given URL
$filename = $url. "bloginfo.";
if ($cache = $this->_cache->get ($filename)) {
$value = $this->_sendRequest ('bloginfo',$options);
if (! PEAR ::isError ($value) && !empty ($this->_cache)) {
$this->_cache->save ($value);
* The tag query allows you to get a list of posts with the given tag
* associated with it. This API query is currently experimental.
function tag($tag, $options = array ())
/* Check for invalid options */
$valid_options = array ('limit','start','format',
'excerptsize','topexcerptsize');
$options = $this->_checkOptions ($options, $valid_options);
if (PEAR ::isError ($options)) {
$options = array ('tag' => $tag);
$filename = " tag.{$tag}";
if ($cache = $this->_cache->get ($filename)) {
$value = $this->_sendRequest ('tag',$options);
if (! PEAR ::isError ($value) && !empty ($this->_cache)) {
$this->_cache->save ($value);
* TopTags lets you retrieve a list of the most popular post tagd
$valid_options = array ('limit','start');
$options = $this->_checkOptions ($options, $valid_options);
if (PEAR ::isError ($options)) {
return PEAR ::raiseError ('You must supply options as an array');
$filename = $url. "cosmos". join("-",$options);
if ($cache = $this->_cache->get ($filename)) {
$value = $this->_sendRequest ('toptags',$options);
if (! PEAR ::isError ($value) && !empty ($this->_cache)) {
$this->_cache->save ($value);
* This lets users retrieve their Attention.XML
* This API query is currently experimental.
$options = array ('username' => $user, 'password' => md5($password));
$filename = " attention.{$user}";
if ($cache = $this->_cache->get ($filename)) {
$value = $this->_sendRequest ('attention',$options);
if (! PEAR ::isError ($value) && !empty ($this->_cache)) {
$this->_cache->save ($value);
* This posts a new Attention.XML file to the Technorati system.
* This API query is currently experimental. This is the one call that
* doesn't use _sendRequest, because it needs to POST a file.
$options = array ('username' => $user, 'password' => md5($password));
$filename = " attention.{$user}";
/* We don't cache this query */
$request = & new HTTP_Request ($this->_apiUrl. "attention");
$request->setMethod (HTTP_REQUEST_METHOD_POST );
$addfile = $request->addFile ("attention.xml",$file);
if (PEAR ::isError ($addfile)) {
$request->addPostData ("username", $user);
$request->addPostData ("password", md5($password));
$request->addHeader ('User-Agent','Services_Technorati');
if ($request->getResponseCode () !== 200 ) {
return PEAR ::raiseError ('Invalid Response Code', $request->getResponseCode ());
$result = $request->getResponseBody ();
$this->_xmlUs = & new XML_Unserializer ();
$this->_xmlUs->setOption ('parseAttributes',true );
$result = $this->_xmlUs->unserialize ($result);
if (PEAR ::isError ($result)) {
if (!empty ($result['document']['result']['error'])) {
return PEAR ::raiseError ("Technorati Response Error",
$value['document']['result']['error']);
$value = $this->_xmlUs->getUnserializedData ();
$this->_cache->save ($value);
* @param string type of query
* @param array parameters
* @return array|PEAR_ERROR
function _sendRequest ($query, $options = array ())
/* Do all the nitty gritty HTTP stuff. Except attentionPost */
$url = sprintf("%s/%s?key=%s",$this->_apiUrl,$query,$this->_apiKey);
foreach ($options as $key => $value) {
$url = $url . '&' . $key . '=' . urlencode($value);
$request = & new HTTP_Request ($url);
$request->addHeader ('User-Agent','Services_Technorati');
if ($request->getResponseCode () !== 200 ) {
return PEAR ::raiseError ('Invalid Response Code',
$request->getResponseCode ());
$result = $request->getResponseBody ();
$this->_xmlUs = & new XML_Unserializer ();
$this->_xmlUs->setOption ('parseAttributes',true );
$result = $this->_xmlUs->unserialize ($result);
if (PEAR ::isError ($result)) {
if (!empty ($result['document']['result']['error'])) {
return PEAR ::raiseError ("Technorati Response Error",
$value['document']['result']['error']);
$unserialized = $this->_xmlUs->getUnserializedData ();
if (!empty ($unserialized['document']['result']['error'])) {
return PEAR ::raiseError ("Technorati Response Error",
$value['document']['result']['error']);
* raise errors if options are specified that aren't allowed
* @param array specified options
* @param array acceptable options
function _checkOptions ($current, $accepted)
foreach ($current as $option => $value) {
$accepted_options[$option] = $value;
PEAR ::raiseError ("Invalid option passed to Query",$option);
Documentation generated on Mon, 11 Mar 2019 14:21:13 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|