Jump to content

GET, Return API(XML)results, then display each returned record in <div></div>


haqker

Recommended Posts

I am looking to accomplish the following but have been hitting a brick wall:

 

    * User enters a single keyword into a text field, and conducts a search keyword search.

    * The results are pulled from the campusbooks.com API(API docs attached)

    * result are then output in <div> class and includes all the book details and its corresponding url./img etc

 

I'm trying to simplify this process but I continue to receive syntax errors.

A step by step practical explanation would do me justice!

 

[attachment deleted by admin]

here's how the code looks thus far, courtesy of 'ignace' (thx)

 

class CampusBooksAPIException extends Exception {}
class CampusBooksGateway
{
    private $apiKey;
    private $apiVersion = '11';
    private $apiURL = 'http://api.campusbooks.com';
    private $apiProtocol = 'rest';
    
    function __construct($apiKey, $apiVersion = '11') {
        $this->apiKey = $apiKey;
        $this->apiVersion = $apiVersion;
    }
    
    function getPrices($isbn) {
        $simpleXML = $this->_query('prices',
            array('key' => $this->apiKey, 'isbn' => $isbn));
    }
    
    function getBookInfo($isbn) {
        $simpleXML = $this->_query('bookinfo',
            array('key' => $this->apiKey, 'isbn' => $isbn));
    }
    
    function searchByAuthor($author, $page, $imageWidth = 50, $imageHeight = 50) {}
    function searchByTitle($title, $page, $imageWidth = 50, $imageHeight = 50) {}
    function searchByKeyword($keyword, $page, $imageWidth = 50, $imageHeight = 50) {}
    function searchAll($input, $page, $imageWidth = 50, $imageHeight = 50) {}
    
    function getBookPrices($isbn) {}
    
    function getBuybackPrices($isbn) {}
    
    function getMerchantsAll() {}
    function getMerchantsBuy() {}
    function getMerchantsBuyback() {}
    
    private function _query($method, array $options) {
        $simpleXML = simplexml_load_file($this->_formatURL($method, $options));
        if($simpleXML === false)
            throw new CampbusBooksAPIException('Campus Books API not available or invalid format.');
        
        if($simpleXML->attributes()->status != 'ok')
            throw new CampbusBooksAPIException($simpleXML->errors->error);
        
        return $simpleXML;
    }
    
    private function _formatURL($method, array $options) {
        return $this->apiURL.'/'.
               $this->apiVersion.'/'.
               $this->apiProtocol.'/'.
               $method.'?'.http_build_query($options);
    }
}
---------
second part (courtesy of 'ignace'(thx)
---------
try {
    $campusBooksAPI = new CampusBooksGateway('api-key-here');
    $books = $campusBooksAPI->getPrices($_GET['ISBN']);
    
    foreach($books as $book)
        echo $book->getAuthor(), "<br>\r\n";
} catch (CampusBooksAPIException $e) {
    echo $e->getMessage();
}

 

 

 

I need a practical explanation from start to end. I continue to receive 'syntax errors'

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.