Public API with PHP, jQuery and JSON

Okay I'm gonna show you a simple way of creating a Public API using PHP, jQuery and JSON.
Demo Let’s get started! First we need to include the connection to the database. This file will be called connection.php
Now for the API. First we include the database connection file. Then we check if the function exists. Now that we checked if the function exists we call the passed function. What’s next? Creating the function it’s self. So we create a function called allUsers(). Select all users from the database, put them into an array then loop through them putting them into a single array. Now for the JSON, we use the PHP json_encode() function.  
Now for the jQuery The url in the $.getJSON() function looks like http://www.YOUR_WEBITE_ADDRESS.com/api.php?method=allUsers&jsoncallback=? the method is set to method=allUsers and the jsoncallback set to &jsoncallback=?. Now we loop through the result an display them in a DIV.  
Now for the final html

Tagged with:

View all contributions by

Website:

Related Articles

Advertisement

Stay connected with us

Add us on Google+

Follow us on Facebook

Follow us on Twitter

Get updates via RSS

Get email updates

FEEDBACK2

  1. _druu
    1

    I hope you see the huge security problem with this code…
    Calling a GET param blindly as a function will kick your butt pretty soon…
    Even though it’s a basic tutorial, just showing how to do something like that, you should’ve added at least a little bit security

    1. Barrett Palmer
      2

      Thanks for the input. I thought of that while making the tutorial. But decided to keep it basic. Just trying to show you how it’s done. I’m not here to teach basic security. There are plenty of tutorials on that all over the web for that. Thanks again :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">