Displaying API results inside a WordPress page template I've created a WordPress page template that calls an API using Httpful and formats the results. This is my first time using APIs on a page, so I'd like to know if this is the right direction to go in.

The code below uses information from a members API to receive the member's information and display their basic information along with a single upcoming class.

page-members.php

&lt;?php
/**
 * The template for displaying a list of members.
 *
 * @package themename
 */

get_header();

// call from api using basic auth
require_once 'inc/httpful.phar';
$uri = "https://api.example.com/v1/accounts/KEY/members_list";
$response = \Httpful\Request::get($uri)
    -&gt;addHeader('Authorization', 'Basic KEY')
    -&gt;expects('application/json')
    -&gt;send();
?&gt;

&lt;main class="members"&gt;
    &lt;section class="members-content"&gt;
        &lt;?php
        $defaultImage = get_field('logo', 'option');

        // loop through api response for each member
        foreach($response-&gt;body-&gt;members as $member) :
            $memberID = $member-&gt;id;

            // if member has no id, skip member
            if (empty($memberID)) :
                continue;
            endif;

            // if member has no image, use default logo
            $memberImage   = (!empty($member-&gt;image)) ? $member-&gt;image : $defaultImage;
            $memberName    = $member-&gt;name;
            $memberTagline = $member-&gt;tagline;

            // loop through upcoming classes for each member
            foreach($member-&gt;upcoming_classes as $class) :
                $classID = $class-&gt;id;

                // if class has no id, skip class
                if (empty($classID)) :
                    continue;
                endif;

                $className = $class-&gt;name;
                $classDate = $class-&gt;date;
                $classTime = $class-&gt;start_time;
            ?&gt;

            &lt;div class="members-column"&gt;
                &lt;a href="&lt;?php echo site_url() . '/member/?MemberID=' . $memberID; ?&gt;"&gt;
                    &lt;div class="members-image" style="background: url('&lt;?php echo $memberImage; ?&gt;') no-repeat top center / cover;"&gt;
                    &lt;/div&gt;
                &lt;/a&gt;

                &lt;div class="members-list"&gt;
                    &lt;h2 class="members-list-name"&gt;&lt;?php echo $memberName; ?&gt;&lt;/h2&gt;
                    &lt;h4 class="members-list-tagline"&gt;&lt;?php echo $memberTagline; ?&gt;&lt;/h4&gt;
                &lt;/div&gt;

                &lt;div class="members-class"&gt;
                    &lt;span class="members-class-info"&gt;
                        &lt;?php echo $className; ?&gt;&lt;br&gt;
                        &lt;!-- format class date, ex: 01/01 - 1:00pm --&gt; 
                        &lt;?php echo date('m/d', strtotime($classDate)); ?&gt; — &lt;?php echo date('g:i a', strtotime($classTime)); ?&gt;
                    &lt;/span&gt;

                    &lt;a class="button" href="&lt;?php echo site_url() . '/reserve/?classID=' . $classID; ?&gt;"&gt;Book&lt;/a&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;?php
        // end class loop by breaking on first upcoming class with an id
        break; endforeach; endforeach;
        ?&gt;
    &lt;/section&gt;
&lt;/main&gt;

&lt;?php get_footer(); ?&gt;


Is there anything I can do to improve this single template?
I see three things

I'm not too terribly familiar with custom wordpress tempaltes, but I'm fairly certain you shouldn avoid doing the API logic in the view. You should call the API in a controller and pass the result to the view, if at all possible. This is a standard separation of concerns.

Additionally, you shouldn't assume the API call will work. If they're server or dns has issues, or they make a breaking change to their API, you should handle it gracefully and inform the user that there are issues. Right now it appears foreach($response-&gt;body-&gt;members as $member) would fail if the API failed.

Finally, it's a good idea to keep the "Basic KEY" in a config or something, so you don't need to touch the code to update the key.