Home » OpenBook WordPress Plugin

OpenBook Plugin: Coding in PHP

27 May 2008 No Comment
This entry is part 5 of 12 in the series Building OpenBook

OpenBook WordPress PluginIn 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.

Index

Series Navigation«OpenBook Plugin: The Open Library APIOpenBook Plugin: Grabbing the Book Data»

Post to Twitter

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.