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]

Link to comment
Share on other sites

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'

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.