For all your music news related needs


Week 3:

An Application Programming Interface (API), in basic terms, is a method of communication between two or more applications. They are incredibly useful and can be used in a multitude of different situations. Another way to look at it is that APIs are a set of rules which allow the programmer to interact with a library, tool, or app. APIs are useful for many reasons, they allow the programmer to set a list of defaults and standards for interfacing with their work. APIs may also be used internally in teams where one person may be building a backend and another may be working on a front end, it means they have a common format for interfacing data over. The most common formats of API are JSON and XML. This is because almost all languages and tools which are used by programmers are built to support these formats, it is universal - of sorts.

Below is a section of code in a file called api_article.php, this code as you can see prepares the select statement for displaying articles on the home page. Additional code will be needed for this to be utilised.

<?php
function displayAllArticles(){

global $con;
$query = $con->prepare('
SELECT article.articleID, article.articleHeadline, article.articleDesc, article.articleImgLink, article.postDate
FROM article
WHERE article.articleDeleted != 1
ORDER BY article.articleID DESC');
$query->execute();
$stmt = $query->fetchAll(PDO::FETCH_OBJ);
return json_encode($stmt);
}
?>

The code above works in parallel with the code below, this is the API in use. JSON is used in this example where the api_article.php holds the query and the index.php (with the function below located inside) uses this API to get information. JSON is encoded and decoded where necessary to fulfil the needs of the API. Loops are used within this in order to fill out and create the cards for the homepage. This function is also used when the dateSearch function is used.

<?php
try {

    $stmt = displayAllArticles();
    $stmtJson = json_decode($stmt);
    for($i=0;$i<sizeof($stmtJson);$i++){
        echo '<div class="col-12 col-sm-4 col-md-4 col-lg-4 align-items-stretch">';
        echo '<div class="card  mb-4 box-shadow h-md-250">';

        echo '<div class="card-body align-items-start row-relative">';
       
        echo '<h3 class="mb-0">';
        echo '<a class="text-dark text-center" href="viewpost.php?id='.$stmtJson[$i] -> articleID.'">'.$stmtJson[$i] -> articleHeadline.'</a>';
        echo '</h3>';
        echo '<div class="mb-1 text-muted">'.date('jS M Y H:i', strtotime($stmtJson[$i] -> postDate)).'</div>';

        echo '<a href="viewpost.php?id='.$stmtJson[$i] -> articleID.'"><img src="'.$stmtJson[$i] -> articleImgLink.'" class="img-responsive w-100"></a>';

        echo '<p class="card-text mb-auto">'.$stmtJson[$i] -> articleDesc.'</p>';
        echo '<a href="viewpost.php?id='.$stmtJson[$i] -> articleID.'">Continue reading</a>';

        echo '</div>';
        echo '</div>';
        echo '</div>';

    }

} catch(PDOException $e) {
    echo $e->getMessage();
}
?>

Regularly, an API interface is setup so that it will continue to work in the long-term regardless of whether the programmer making use of the API changes their front-end. Companies often create APIs for their tools, applications, or systems which never have backend changes, this means the people who use said API will not have to constantly adjust their code in order to flex around constant changes in interfaces. This is the main attraction to the usage of APIs, one can use an API and trust that it will function in the same manor for the foreseeable future. Some advantages to using APIs include the ease of integration, one can easily embed content from an API onto their system often by simply copying and pasting a line or two of code. Other advantages include efficiency where you should be able to expect an efficiency and fully refactored interface.

word count: 429

sources: https://www.programmableweb.com/news/what-are-benefits-apis/analysis/2015/12/03 https://medium.com/@TebbaVonMathenstien/what-is-an-api-and-why-should-i-use-one-863c3365726b