haqker Posted November 14, 2010 Share Posted November 14, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/ Share on other sites More sharing options...
ignace Posted November 14, 2010 Share Posted November 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/#findComment-1134106 Share on other sites More sharing options...
ignace Posted November 14, 2010 Share Posted November 14, 2010 The reason I didn't include any code is because I do not know how you will handle/use it or in what way you would like to see it Object, array, ..? Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/#findComment-1134111 Share on other sites More sharing options...
haqker Posted November 14, 2010 Author Share Posted November 14, 2010 Wow, you're a life saver! I prefer an object display, however an array would suffice. whichever of the two is more beneficial and less of a strain on bandwidth etc. What would you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/#findComment-1134120 Share on other sites More sharing options...
haqker Posted November 14, 2010 Author Share Posted November 14, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/#findComment-1134132 Share on other sites More sharing options...
haqker Posted November 14, 2010 Author Share Posted November 14, 2010 gradbay.com/bookindex.php shows the error i am getting Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/#findComment-1134142 Share on other sites More sharing options...
ignace Posted November 14, 2010 Share Posted November 14, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/#findComment-1134150 Share on other sites More sharing options...
haqker Posted November 14, 2010 Author Share Posted November 14, 2010 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! :-\ Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/#findComment-1134205 Share on other sites More sharing options...
ignace Posted November 15, 2010 Share Posted November 15, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/218633-php-api-xml/#findComment-1134612 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.