OpenBook Plugin: Coding in PHP
- When You Link to a Book, Do You Link to Amazon, LibraryThing, WorldCat, or What?
- Four Big Libraries: I Pick OpenLibrary
- OpenBook Plugin: Requirements
- OpenBook Plugin: The Open Library API
- OpenBook Plugin: Coding in PHP
- OpenBook Plugin: Grabbing the Book Data
- OpenBook Plugin: Let There Be Cover Images!
- OpenBook Plugin: The Importance of Linking to Publisher Websites
- OpenBook Plugin: Graphic Design
- OpenBook Plugin: Hooking into WordPress
- OpenBook Plugin: Now Accepting Arguments, and Beta Testers
- OpenBook Beta Release!
In the previous post, I retrieved book data in my browser using the API of Open Library. In this post, I capture that data in PHP. I’m not going to try to teach PHP, just give you a feel for how it works.
I will perform three steps. First, I define a variable, “$url”, that matches the API URL created in the last post. Second, I pass the URL to a PHP function, “file_get_contents”; this function scoops the book data from OpenLibrary and places it in another variable, $bookdata. In the next post, I will use PHP to select specific values from that variable, but for now I’m just going to print everything to the screen using the PHP “echo” function. Here’s the code:
$url = “http://openlibrary.org/api/get?key=/b/OL3674869M&text=true&prettyprint=true”;
$bookdata = file_get_contents($url);
echo $bookdata;
PHP code has to be wrapped in opening and closing tags in order for it to run. I have not done that here so you can view the code. If you click here, a page will open that contains the same code with the required tags. You will not see the code there. It will be processed on the server and display the book data, the same data you saw in the last post. The difference is that it has been captured in PHP so I can start programming it. In the next post, I will use PHP to select values from the book data, the ones that will be displayed in blog posts. There’s a trick to that, but PHP makes it easy.
Note. If you want to code PHP on your WordPress server, and you have purchased a website from a host for your WordPress installation, then you are ready to go. You can code PHP in text files with a .php extension right on your web server. I am coding right in my WordPress posts, but that required a plugin, Exec-PHP.





Leave your response!