preload
0 Comments | Oct 05, 2011

Using APIs in PHP

Estimated Time To Read This: 2 – 4 minutes      


I wasn’t really sure where to start with this blog post. I’ve been writing articles and blog posts for CoreSolutions for a while, but they’ve mostly been on the FileMaker side of things, which really represents about half my job – for a good chunk of the last decade I’ve been splitting my development time between both FileMaker and PHP. Granted, my PHP development really came out of the need to pull data from a FileMaker database into a website, but I’ve gotten fairly comfortable with using PHP with other databases (mostly MySQL, with a little bit of SQL Server thrown in here and there). But I haven’t really had to sit down and write about it before.

I thought I’d ease into it a bit and stick with one of the topics that we’ve been covering on the blog up to this point – WordPress – but come at it from a slightly different angle, and talk a bit about the use if APIs. On a recent project, I needed to be able to push information from an existing website into a new WordPress based site that I was working on; this is the sort of thing that API’s are made for. An API is basically a set of functions that let you interact with another piece of software. Ever see a button on a website that lets you share that page on Facebook, or Twitter? The website is using Facebook’s (or Twitter’s, or Google’s) API to connect to those services from their website.

So how do you use an API? There really isn’t a quick answer, since every API is different; you’ll need to check the documentation (sites like Google and Facebook tend to have pretty good documentation on how to use their APIs). I’ll return to my example above about posting to a WordPress site. WordPress supports a couple of different Blogging APIs (Blogger, metaWeblog, Moveable Type). I did a bit of digging and found some examples on how to use the metaWeblog API; I’ll post my code below. Note that the code uses cURL to send the Post to WordPress and get back a response; if you haven’t used cURL I’d suggest reading up a bit on it (http://php.net/manual/en/intro.curl.php), it’s used a lot in using APIs.



Tags:, , ,





Related Articles


Leave a Reply

* Required
** Your Email is never shared