Jump to content

PHP API XML


haqker

Recommended Posts

I am trying to implement an API (XML data) into a an existing site.

The site operates as an online bookstore.

Id like to attach a style to the XML feed.

Ideally, I would like to incorporate the results from the campusbooks.com API into my existing search engine(of my local inventory only) I am stumped. :-/

I have attached the API documentation.

Any help would be great.

Thank you.

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Wrap the campusbooks api into a class like so:

 

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);
    }
}

 

Write the missing code and use this interface to communicate with the API you will have to visit each URL though to look at the response it generates to write the code to handle it.

Link to comment
Share on other sites

The class code looks great!, however, getting the information to display  is the confusing part.

 

also, when i defined the API key $apikey = 'the key value' i received a T_string error :-/

 

This is my starting point... thoughts?

 

 

<form action="bookindex.php" method="get">

  <p>

    <input name="input" type="text" value="search all" size="35" maxlength="35" />

   

  </p>

  <p>

    <label for="search">search</label>

    <input type="submit" name="searchAll" id="searchAll" value="Submit" />

  </p>

</form>

 

 

 

 

Link to comment
Share on other sites

The class once you have implemented the necessary code would be used like:

 

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 highly recommend you use domain classes that know of the format (SimpleXMLElement) used by CampusBooksGateway like a BookList class of some sort this will make your program more flexible then if you would use the SimpleXMLElement object directly in your client (not to mention that you would break encapsulation, whatever that's worth to you!

Link to comment
Share on other sites

Thank you! I  truly appreciate your expertise! excuse my 'newbieness' lol!

 

Ok Ive implemented the second portion of the code (after including the appropriate code), however i continue to get a syntax error.

 

Ultimately, what I am looking to accomplish is the following:

 

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

[*]The results are pulled from the campusbooks API

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

 

Im trying to simply this process but I continue to receive syntax errors. A step by step practical explanation would do me justice!

:-\

 

Link to comment
Share on other sites

A syntax error means that I/you made a typo/forgot to add something in the code. I'll need to see your code to help you! Don't pardon yourself for newbieness, if you don't understand something just say so and ask to clarify.

 

The results are pulled from the campusbooks API

 

try {
    $gateway = new CampusBooksAPI_Gateway('api-key-here');
    $bookList = $gateway->search($_GET['query']);
    
    echo '<table><tr><th>Title</th><th>Author</th></tr>';
    foreach($bookList as $book) {
        echo '<tr><td>', $book->getTitle(), '</td></tr><tr><td>', $book->getAuthor(), '</td></tr>';
    }
    echo '</table>';
} catch(CampusBooksAPIException $e) {
    echo $e->getMessage();
}

 

Possible implementation:

 

function search($query) {
    $bookList = new BookList();
    $response = $this->_getResponseXML('search', array('title' => $query, 'author' => $query, 'keyword' => $query));
    foreach($response->page->results->book as $book) {
        $data = array('isbn' => $book->isbn13,
                      'title' => $book->title,
                      'author' => $book->author,
                      'binding' => $book->binding,
                      'price' => $book->msrp,
                      'pages' => $book->pages,
                      'publisher' => $book->publisher,
                      'published_date' => $book->published_date,
                      'edition' => $book->edition,
                      'description' => $book->description);
        $bookList->add(new Book($data));
    }
    return $bookList;
}

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.