So I got bored and decided it would be a good idea to build a Rest API for my website crappypuns.com. Having only briefly worked with Rest API’s once before I have no doubt that the API I have ended up building is not the best. It is however a simple to implement and understand API so could serve as a good starting point for working with or building API’s. The API and documentation for it are available at http://crappypuns.com/about.php.

The process for creating the API was surprisingly easy and required only two PHP files to create as there are only two commands exposed, one to query for puns and the other to query for images. Each query supports a common parameter ‘limit’ which specifies how many results should be returned up to a maximum of 20.

For the sake of simplicity I will focus only on http://crappypuns.com/api/api_pun.php which is the API link to get jokes. You can choose how many jokes you want returned from the server using the limit parameter like: http://crappypuns.com/api/api_pun.php?limit=10 will return 10 puns in JSON format.

The code that runs on the server when the API call is made is as follows (with minor tweaks for security).

<?php               
        //This is the function that will query the database for the puns
        function getPuns($connect, $limit){                                            
                //Get the amount of jokes required randomly from the database using fake sql parametrs for this example  
                $joke = mysqli_query($connect, "SELECT `punField`,`punIdField` FROM punTable ORDER BY RAND() LIMIT $limit");
                                                                      
                //fetch each result that was returned               
                while($row = mysqli_fetch_assoc($joke)){                 
                        //Append the data to $output               
                        $output[]=$row;                   
                }                                  
                //return the puns pulled from the databse       
                return $output;                    
        }                                             
                                                    
        //See if a limit parameter is set. If not set the limit to 1       
        if (isset($_GET['limit'])){                       
                $limit = $_GET['limit'];               
        }else{$limit = 1;}                      
                                                               
        //make sure limit does not exceed max of 20           
        if($limit > 20){$limit = 20;}                       
                                                         
        //Establish a connection to the database     
        $connect = mysqli_connect('server', 'usrname', 'password', 'database');
        
        //Get the punss from the database                                 
        $jokes = getPuns($connect, $limit);                         
                                                                               
        //Return the results in json format                                 
        print(json_encode($jokes, JSON_UNESCAPED_SLASHES));                  
?> 

I think this is probably the most simple an API can get. Ideally you would expand on this by having some form of security in place like a validation key required to make requests. Maybe a limit on how many requests a client can make in a given time frame.