Kategorien
PHP Zend Framework

Zend APIs: einzelne Komponenten verwenden

Das Zend Framework enthält viele praktische Schnittstellen zu bekannten APIs:

  1. Google Base (Zend_Gdata)
  2. Open ID (Zend_OpenId)
  3. Zend_Service mit:
  • Zend_Service_Akismet
  • Zend_Service_Amazon
  • Zend_Service_Amazon_EC2
  • Zend_Service_Amazon_S3
  • Zend_Service_Audioscrobbler
  • Zend_Service_Delicious
  • Zend_Service_DeveloperGarden
  • Zend_Service_Ebay
  • Zend_Service_Ebay_Finding
  • Zend_Service_Flickr
  • Zend_Service_LiveDocx
  • Zend_Service_Nirvanix
  • Zend_Service_ReCaptcha
  • Zend_Service_ShortUrl
  • Zend_Service_Simpy
  • Zend_Service_SlideShare
  • Zend_Service_StrikeIron
  • Zend_Service_Technorati
  • Zend_Service_Twitter
  • Zend_Service_WindowsAzure
  • Zend_Service_Yahoo

Eine komplette Liste gibt es hier.

Solche APIs können folgender Maßen eingebunden werden ohne die ganze Webseite als Zend-Projekt programmieren zu müssen (am Bsp. von GBase: Google Base):

set_include_path(get_include_path()
            .':/var/www/Zend/'
            .':/var/www/'
        );
require_once '../functions.php';
require_once 'Loader/Autoloader.php';
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->setFallbackAutoloader(true);

class GoogleShopping {
    private $service = null;
    public function  __construct()
    {
         // Parameters for ClientAuth authentication
         $service = Zend_Gdata_Gbase::AUTH_SERVICE_NAME;
         $user = "foo";
         $pass = "doo";

         // Create an authenticated HTTP client
         $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);

         // Create an instance of the Base service
         try
         {
             $this->service = new Zend_Gdata_Gbase($client);
             $this->addProduct();
             $this->queryFeed();
         }
         catch (Exception $exception)
         {
            $this->printStacktrace($exception);
         }
    }
    private function printStacktrace($exception)
    {
        $msg = $exception->getMessage();
        $trace = $exception->getTraceAsString();
        echo "<div>Error: $msg<p><pre>$trace</pre></p></div>";
    }
    private function addProduct()
    {
         $newEntry = $this->service->newItemEntry();

         // Add title
         $title = "PHP Developer Handbook";
         $newEntry->title = $this->service->newTitle(trim($title));

         // Add some content
         $content = "Essential handbook for PHP developers.";
        $newEntry->content = $this->service->newContent($content);
        $newEntry->content->type = 'text';

        // Define product type
        $itemType = "Products";
        $newEntry->itemType = $itemType;

        // Add item specific attributes
        $newEntry->addGbaseAttribute("product_type", "book", "text");
        $newEntry->addGbaseAttribute("price", "12.99 USD", "floatUnit");
        $newEntry->addGbaseAttribute("quantity", "10", "int");
        $newEntry->addGbaseAttribute("weight", "2.2 lbs", "numberUnit");
        $newEntry->addGbaseAttribute("condition", "New", "text");
        $newEntry->addGbaseAttribute("author", "John Doe", "text");
        $newEntry->addGbaseAttribute("edition", "First Edition", "text");
        $newEntry->addGbaseAttribute("pages", "253", "number");
        $newEntry->addGbaseAttribute("publisher", "My Press", "text");
        $newEntry->addGbaseAttribute("year", "2007", "number");
        $newEntry->addGbaseAttribute("payment_accepted", "Google Checkout", "text");

        $dryRun = true;
        $createdEntry = $this->service->insertGbaseItem($newEntry, $dryRun);        
    }
    private function queryFeed()
    {
         $query = $this->service->newItemQuery();
         $query->setBq('[title:Programming]');
         $query->setOrderBy('modification_time');
         $query->setSortOrder('descending');
         $query->setMaxResults('5');
         $feed = $this->service->getGbaseItemFeed($query);
         $this->displayFeed($feed);
    }
    private function displayFeed($feed)
    {
         foreach ($feed->entries as $entry) {
           // Get all attributes and print out the name and text value of each
           // attribute
           $baseAttributes = $entry->getGbaseAttributes();
           foreach ($baseAttributes as $attr) {
             echo "Attribute " . $attr->name . " : " . $attr->text . "<br>";
           }
         }
    }

}