PHP API Tutorial

So you have a website and you want to integrate that site with mobile applications and other neat things. You’re going to need to build an API for your website, but no worries! It’s easy to build an API that returns JSON data back to your applications.

Usually you would want to store your API keys in a database, but to keep things simple I’m going to hard-code the API key right into my example.

Assuming that your website is example.com, and the api script is located at /api.php, when you visit //example.com/api.php?request=API-Key/example you should get this response.

If you modify json_encode($return); to json_encode($return, JSON_PRETTY_PRINT); then you will get nicer looking response such as this.

 

I’m a geek, gamer, and writer.
I am also an apache attack helicopter. Hobbies include flying around, shooting things down, and occasionally breaking stuff.