Zend_Service_StrikeIron: Advanced Uses

Zend_Service_Technorati

Introduction

Zend_Service_Technorati provides an easy, intuitive and object-oriented interface for using the Technorati API. It provides access to all available » Technorati API queries and returns the original XML response as a friendly PHP object.

» Technorati is one of the most popular blog search engines. The API interface enables developers to retrieve information about a specific blog, search blogs matching a single tag or phrase and get information about a specific author (blogger). For a full list of available queries please see the » Technorati API documentation or the Available Technorati queries section of this document.

Getting Started

Technorati requires a valid API key for usage. To get your own API Key you first need to » create a new Technorati account, then visit the » API Key section.

Note: API Key limits
You can make up to 500 Technorati API calls per day, at no charge. Other usage limitations may apply, depending on the current Technorati API license.

Once you have a valid API key, you're ready to start using Zend_Service_Technorati.

Making Your First Query

In order to run a query, first you need a Zend_Service_Technorati instance with a valid API key. Then choose one of the available query methods, and call it providing required arguments.

Example #1 Sending your first query

  1. // create a new Zend_Service_Technorati
  2. // with a valid API_KEY
  3. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  4.  
  5. // search Technorati for PHP keyword
  6. $resultSet = $technorati->search('PHP');

Each query method accepts an array of optional parameters that can be used to refine your query.

Example #2 Refining your query

  1. // create a new Zend_Service_Technorati
  2. // with a valid API_KEY
  3. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  4.  
  5. // filter your query including only results
  6. // with some authority (Results from blogs with a handful of links)
  7. $options = array('authority' => 'a4');
  8.  
  9. // search Technorati for PHP keyword
  10. $resultSet = $technorati->search('PHP', $options);

A Zend_Service_Technorati instance is not a single-use object. That is, you don't need to create a new instance for each query call; simply use your current Zend_Service_Technorati object as long as you need it.

Example #3 Sending multiple queries with the same Zend_Service_Technorati instance

  1. // create a new Zend_Service_Technorati
  2. // with a valid API_KEY
  3. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  4.  
  5. // search Technorati for PHP keyword
  6. $search = $technorati->search('PHP');
  7.  
  8. // get top tags indexed by Technorati
  9. $topTags = $technorati->topTags();

Consuming Results

You can get one of two types of result object in response to a query.

The first group is represented by Zend_Service_Technorati_*ResultSet objects. A result set object is basically a collection of result objects. It extends the basic Zend_Service_Technorati_ResultSet class and implements the SeekableIterator PHP interface. The best way to consume a result set object is to loop over it with the PHP foreach() statement.

Example #4 Consuming a result set object

  1. // create a new Zend_Service_Technorati
  2. // with a valid API_KEY
  3. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  4.  
  5. // search Technorati for PHP keyword
  6. // $resultSet is an instance of Zend_Service_Technorati_SearchResultSet
  7. $resultSet = $technorati->search('PHP');
  8.  
  9. // loop over all result objects
  10. foreach ($resultSet as $result) {
  11.     // $result is an instance of Zend_Service_Technorati_SearchResult
  12. }

Because Zend_Service_Technorati_ResultSet implements the SeekableIterator interface, you can seek a specific result object using its position in the result collection.

Example #5 Seeking a specific result set object

  1. // create a new Zend_Service_Technorati
  2. // with a valid API_KEY
  3. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  4.  
  5. // search Technorati for PHP keyword
  6. // $resultSet is an instance of Zend_Service_Technorati_SearchResultSet
  7. $resultSet = $technorati->search('PHP');
  8.  
  9. // $result is an instance of Zend_Service_Technorati_SearchResult
  10. $resultSet->seek(1);
  11. $result = $resultSet->current();

Note: SeekableIterator works as an array and counts positions starting from index 0. Fetching position number 1 means getting the second result in the collection.

The second group is represented by special standalone result objects. Zend_Service_Technorati_GetInfoResult, Zend_Service_Technorati_BlogInfoResult and Zend_Service_Technorati_KeyInfoResult act as wrappers for additional objects, such as Zend_Service_Technorati_Author and Zend_Service_Technorati_Weblog.

Example #6 Consuming a standalone result object

  1. // create a new Zend_Service_Technorati
  2. // with a valid API_KEY
  3. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  4.  
  5. // get info about weppos author
  6. $result = $technorati->getInfo('weppos');
  7.  
  8. $author = $result->getAuthor();
  9. echo '<h2>Blogs authored by ' . $author->getFirstName() . " " .
  10.           $author->getLastName() . '</h2>';
  11. echo '<ol>';
  12. foreach ($result->getWeblogs() as $weblog) {
  13.     echo '<li>' . $weblog->getName() . '</li>';
  14. }
  15. echo "</ol>";

Please read the Zend_Service_Technorati Classes section for further details about response classes.

Handling Errors

Each Zend_Service_Technorati query method throws a Zend_Service_Technorati_Exception exception on failure with a meaningful error message.

There are several reasons that may cause a Zend_Service_Technorati query to fail. Zend_Service_Technorati validates all parameters for any query request. If a parameter is invalid or it contains an invalid value, a new Zend_Service_Technorati_Exception exception is thrown. Additionally, the Technorati API interface could be temporally unavailable, or it could return a response that is not well formed.

You should always wrap a Technorati query with a try ... catch block.

Example #7 Handling a Query Exception

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. try {
  3.     $resultSet = $technorati->search('PHP');
  4. } catch(Zend_Service_Technorati_Exception $e) {
  5.     echo "An error occurred: " $e->getMessage();
  6. }

Checking Your API Key Daily Usage

From time to time you probably will want to check your API key daily usage. By default Technorati limits your API usage to 500 calls per day, and an exception is returned by Zend_Service_Technorati if you try to use it beyond this limit. You can get information about your API key usage using the Zend_Service_Technorati::keyInfo() method.

Zend_Service_Technorati::keyInfo() returns a Zend_Service_Technorati_KeyInfoResult object. For full details please see the » API reference guide.

Example #8 Getting API key daily usage information

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $key = $technorati->keyInfo();
  3.  
  4. echo "API Key: " . $key->getApiKey() . "<br />";
  5. echo "Daily Usage: " . $key->getApiQueries() . "/" .
  6.      $key->getMaxQueries() . "<br />";

Available Technorati Queries

Zend_Service_Technorati provides support for the following queries:

Technorati Cosmos

» Cosmos query lets you see what blogs are linking to a given URL. It returns a Zend_Service_Technorati_CosmosResultSet object. For full details please see Zend_Service_Technorati::cosmos() in the » API reference guide.

Example #9 Cosmos Query

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $resultSet = $technorati->cosmos('http://devzone.zend.com/');
  3.  
  4. echo "<p>Reading " . $resultSet->totalResults() .
  5.      " of " . $resultSet->totalResultsAvailable() .
  6.      " available results</p>";
  7. echo "<ol>";
  8. foreach ($resultSet as $result) {
  9.     echo "<li>" . $result->getWeblog()->getName() . "</li>";
  10. }
  11. echo "</ol>";

Technorati Search

The » Search query lets you see what blogs contain a given search string. It returns a Zend_Service_Technorati_SearchResultSet object. For full details please see Zend_Service_Technorati::search() in the » API reference guide.

Example #10 Search Query

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $resultSet = $technorati->search('zend framework');
  3.  
  4. echo "<p>Reading " . $resultSet->totalResults() .
  5.      " of " . $resultSet->totalResultsAvailable() .
  6.      " available results</p>";
  7. echo "<ol>";
  8. foreach ($resultSet as $result) {
  9.     echo "<li>" . $result->getWeblog()->getName() . "</li>";
  10. }
  11. echo "</ol>";

Technorati Tag

The » Tag query lets you see what posts are associated with a given tag. It returns a Zend_Service_Technorati_TagResultSet object. For full details please see Zend_Service_Technorati::tag() in the » API reference guide.

Example #11 Tag Query

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $resultSet = $technorati->tag('php');
  3.  
  4. echo "<p>Reading " . $resultSet->totalResults() .
  5.      " of " . $resultSet->totalResultsAvailable() .
  6.      " available results</p>";
  7. echo "<ol>";
  8. foreach ($resultSet as $result) {
  9.     echo "<li>" . $result->getWeblog()->getName() . "</li>";
  10. }
  11. echo "</ol>";

Technorati DailyCounts

The » DailyCounts query provides daily counts of posts containing the queried keyword. It returns a Zend_Service_Technorati_DailyCountsResultSet object. For full details please see Zend_Service_Technorati::dailyCounts() in the » API reference guide.

Example #12 DailyCounts Query

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $resultSet = $technorati->dailyCounts('php');
  3.  
  4. foreach ($resultSet as $result) {
  5.     echo "<li>" . $result->getDate() .
  6.          "(" . $result->getCount() . ")</li>";
  7. }
  8. echo "</ol>";

Technorati TopTags

The » TopTags query provides information on top tags indexed by Technorati. It returns a Zend_Service_Technorati_TagsResultSet object. For full details please see Zend_Service_Technorati::topTags() in the » API reference guide.

Example #13 TopTags Query

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $resultSet = $technorati->topTags();
  3.  
  4. echo "<p>Reading " . $resultSet->totalResults() .
  5.      " of " . $resultSet->totalResultsAvailable() .
  6.      " available results</p>";
  7. echo "<ol>";
  8. foreach ($resultSet as $result) {
  9.     echo "<li>" . $result->getTag() . "</li>";
  10. }
  11. echo "</ol>";

Technorati BlogInfo

The » BlogInfo query provides information on what blog, if any, is associated with a given URL. It returns a Zend_Service_Technorati_BlogInfoResult object. For full details please see Zend_Service_Technorati::blogInfo() in the » API reference guide.

Example #14 BlogInfo Query

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $result = $technorati->blogInfo('http://devzone.zend.com/');
  3.  
  4. echo '<h2><a href="' . (string) $result->getWeblog()->getUrl() . '">' .
  5.      $result->getWeblog()->getName() . '</a></h2>';

Technorati BlogPostTags

The » BlogPostTags query provides information on the top tags used by a specific blog. It returns a Zend_Service_Technorati_TagsResultSet object. For full details please see Zend_Service_Technorati::blogPostTags() in the » API reference guide.

Example #15 BlogPostTags Query

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $resultSet = $technorati->blogPostTags('http://devzone.zend.com/');
  3.  
  4. echo "<p>Reading " . $resultSet->totalResults() .
  5.      " of " . $resultSet->totalResultsAvailable() .
  6.      " available results</p>";
  7. echo "<ol>";
  8. foreach ($resultSet as $result) {
  9.     echo "<li>" . $result->getTag() . "</li>";
  10. }
  11. echo "</ol>";

Technorati GetInfo

The » GetInfo query tells you things that Technorati knows about a member. It returns a Zend_Service_Technorati_GetInfoResult object. For full details please see Zend_Service_Technorati::getInfo() in the » API reference guide.

Example #16 GetInfo Query

  1. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  2. $result = $technorati->getInfo('weppos');
  3.  
  4. $author = $result->getAuthor();
  5. echo "<h2>Blogs authored by " . $author->getFirstName() . " " .
  6.      $author->getLastName() . "</h2>";
  7. echo "<ol>";
  8. foreach ($result->getWeblogs() as $weblog) {
  9.     echo "<li>" . $weblog->getName() . "</li>";
  10. }
  11. echo "</ol>";

Technorati KeyInfo

The KeyInfo query provides information on daily usage of an API key. It returns a Zend_Service_Technorati_KeyInfoResult object. For full details please see Zend_Service_Technorati::keyInfo() in the » API reference guide.

Zend_Service_Technorati Classes

The following classes are returned by the various Technorati queries. Each Zend_Service_Technorati_*ResultSet class holds a type-specific result set which can be easily iterated, with each result being contained in a type result object. All result set classes extend Zend_Service_Technorati_ResultSet class and implement the SeekableIterator interface, allowing for easy iteration and seeking to a specific result.

Note: Zend_Service_Technorati_GetInfoResult, Zend_Service_Technorati_BlogInfoResult and Zend_Service_Technorati_KeyInfoResult represent exceptions to the above because they don't belong to a result set and they don't implement any interface. They represent a single response object and they act as a wrapper for additional Zend_Service_Technorati objects, such as Zend_Service_Technorati_Author and Zend_Service_Technorati_Weblog.

The Zend_Service_Technorati library includes additional convenient classes representing specific response objects. Zend_Service_Technorati_Author represents a single Technorati account, also known as a blog author or blogger. Zend_Service_Technorati_Weblog represents a single weblog object, along with all specific weblog properties such as feed URLs or blog name. For full details please see Zend_Service_Technorati in the » API reference guide.

Zend_Service_Technorati_ResultSet

Zend_Service_Technorati_ResultSet is the most essential result set. The scope of this class is to be extended by a query-specific child result set class, and it should never be used to initialize a standalone object. Each of the specific result sets represents a collection of query-specific Zend_Service_Technorati_Result objects.

Zend_Service_Technorati_ResultSet implements the PHP SeekableIterator interface, and you can iterate all result objects via the PHP foreach() statement.

Example #17 Iterating result objects from a resultset collection

  1. // run a simple query
  2. $technorati = new Zend_Service_Technorati('VALID_API_KEY');
  3. $resultSet = $technorati->search('php');
  4.  
  5. // $resultSet is now an instance of
  6. // Zend_Service_Technorati_SearchResultSet
  7. // it extends Zend_Service_Technorati_ResultSet
  8. foreach ($resultSet as $result) {
  9.     // do something with your
  10.     // Zend_Service_Technorati_SearchResult object
  11. }

Zend_Service_Technorati_CosmosResultSet

Zend_Service_Technorati_CosmosResultSet represents a Technorati Cosmos query result set.

Note: Zend_Service_Technorati_CosmosResultSet extends Zend_Service_Technorati_ResultSet.

Zend_Service_Technorati_SearchResultSet

Zend_Service_Technorati_SearchResultSet represents a Technorati Search query result set.

Note: Zend_Service_Technorati_SearchResultSet extends Zend_Service_Technorati_ResultSet.

Zend_Service_Technorati_TagResultSet

Zend_Service_Technorati_TagResultSet represents a Technorati Tag query result set.

Note: Zend_Service_Technorati_TagResultSet extends Zend_Service_Technorati_ResultSet.

Zend_Service_Technorati_DailyCountsResultSet

Zend_Service_Technorati_DailyCountsResultSet represents a Technorati DailyCounts query result set.

Note: Zend_Service_Technorati_DailyCountsResultSet extends Zend_Service_Technorati_ResultSet.

Zend_Service_Technorati_TagsResultSet

Zend_Service_Technorati_TagsResultSet represents a Technorati TopTags or BlogPostTags queries result set.

Note: Zend_Service_Technorati_TagsResultSet extends Zend_Service_Technorati_ResultSet.

Zend_Service_Technorati_Result

Zend_Service_Technorati_Result is the most essential result object. The scope of this class is to be extended by a query specific child result class, and it should never be used to initialize a standalone object.

Zend_Service_Technorati_CosmosResult

Zend_Service_Technorati_CosmosResult represents a single Technorati Cosmos query result object. It is never returned as a standalone object, but it always belongs to a valid Zend_Service_Technorati_CosmosResultSet object.

Note: Zend_Service_Technorati_CosmosResult extends Zend_Service_Technorati_Result.

Zend_Service_Technorati_SearchResult

Zend_Service_Technorati_SearchResult represents a single Technorati Search query result object. It is never returned as a standalone object, but it always belongs to a valid Zend_Service_Technorati_SearchResultSet object.

Note: Zend_Service_Technorati_SearchResult extends Zend_Service_Technorati_Result.

Zend_Service_Technorati_TagResult

Zend_Service_Technorati_TagResult represents a single Technorati Tag query result object. It is never returned as a standalone object, but it always belongs to a valid Zend_Service_Technorati_TagResultSet object.

Note: Zend_Service_Technorati_TagResult extends Zend_Service_Technorati_Result.

Zend_Service_Technorati_DailyCountsResult

Zend_Service_Technorati_DailyCountsResult represents a single Technorati DailyCounts query result object. It is never returned as a standalone object, but it always belongs to a valid Zend_Service_Technorati_DailyCountsResultSet object.

Note: Zend_Service_Technorati_DailyCountsResult extends Zend_Service_Technorati_Result.

Zend_Service_Technorati_TagsResult

Zend_Service_Technorati_TagsResult represents a single Technorati TopTags or BlogPostTags query result object. It is never returned as a standalone object, but it always belongs to a valid Zend_Service_Technorati_TagsResultSet object.

Note: Zend_Service_Technorati_TagsResult extends Zend_Service_Technorati_Result.

Zend_Service_Technorati_GetInfoResult

Zend_Service_Technorati_GetInfoResult represents a single Technorati GetInfo query result object.

Zend_Service_Technorati_BlogInfoResult

Zend_Service_Technorati_BlogInfoResult represents a single Technorati BlogInfo query result object.

Zend_Service_Technorati_KeyInfoResult

Zend_Service_Technorati_KeyInfoResult represents a single Technorati KeyInfo query result object. It provides information about your Technorati API Key daily usage.


Zend_Service_StrikeIron: Advanced Uses