Displaying your content from sermonaudio.com just got easier. I first read about their JSON API about a year ago, and naturally I was pretty excited. I’ve personally benefited a ton from the preaching of men such as Joe Morecraft, Bojidar Marinov, Todd Ruddell, Jeff Noblit, Steven Lawson, etc, and I thought it was only fitting that I create a way to give back. At different times over the past year or so I’ve worked on an API for PHP that allows you to do just that, you can view an example of a small app that I built in Angularjs and PHP here. I’ll explain how to use it in depth below.
Setup and Installation
First thing is first, your church must be registered as a full member on sermonaudio.com as in order to attain an API key. If you already use SA to publish your church’s sermons then this shouldn’t be a problem, otherwise you can register here.
Once you’ve created an account, you’ll need to setup an API key in order to have access to your church’s sermons, speakers, events, and languages; you can do that here.
Now that you’ve got your API key, all you need to do is download my repo from github that will connect you to the platform.
After you’ve downloaded or cloned the repo you can then install it by including it in your project
include_once( 'directory_to_api/SermonAudioApi.php' );
Making the connection
Now that you have the API included in your project you can begin setting it up. First setup your API Key using the setApiKey() method.
$sermon_audio_api- = new CarlosRios\SermonAudioApi;
$sermon_audio_api->setApiKey( 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' );
You can test your API key to see if it’s accepted by running the getSermons() method which should return an array of sermon objects if the key was accepted, and false if it was rejected.
var_dump( $sermon_audio_api->getSermons() );
Using the PHP API
Handling the sermons
Once you’ve verified that your key is accepted you can begin by grabbing sermons like we did above. The getSermons() method is a helper method included in the SermonAudioApi class that will build the api request with the corresponding options, and then make the request. The getSermons() method accepts an array of options.
speaker: the name of the speaker exactly as it is on SermonAudio
event: a specific type of event that is associated with sermons, ex ( Sunday Service ).
series: the name of the sermon series as it is on SermonAudio
page: the page number to retrieve the sermons from. Set to “total” to retrieve all sermons
sermons_per_page: the number of sermons to display per page, max is currently 100
year: narrows the sermons down to a specific year
$args = array(
"speaker" => "speakers_name",
"event" => "event_type",
"series" => "series_name",
"page" => "page_number",
"sermons_per_page" => 10,
"year" => 2016
$sermons = $sermon_audio_api->getSermons( $args );
Using the sermons after you’ve made the api request is as easy as running a loop. Each sermon will return with the fields provided by sermon audio in a standard php object.
Handling the Speakers
SermonAudio allows us to grab all of the speakers listed in your SermonAudio account. To do so all we need to do is make the proper request, and SA will send us a list of the speakers.
After we have a list of the speakers, we can loop through them as well.
Handling the Events
Events are really short for event types. SA allows you to group your sermons by event type in their dashboard which allows you to distinguish between something like a 9:00am service and an 11:00am service. To access the types of events you’ve registered with SA, all we need to do is make the proper request.
Handling the Languages
Lastly, to get the languages you’ve registered with SA, all we use the getLanguages() method
Using your own methods to make server requests
By default the PHP API for SermonAudio comes with a nifty requestData() method which is used to make all of the server requests to SA. The requests are made using the file_get_contents function provided with PHP, which happens to work on almost all server configurations. After the requests are made and the JSON is collected, it is then converted into standard PHP objects which can then be used in your application.
Not satisfied with file_get_contents? Well there’s no need to worry, the API has also been prepared in case you would rather handle the requests and parsing of the data yourself. All of the get methods have a route method that they utilize to build the request url before the request is actually made. You can use any of the methods below to get the url where you can make the request yourself and handle the data as you please.
sermonsApiRoute( $args );
Builds the url to grab the sermons, and uses all of the same arguments used in the getSermons method.
Builds the url to grab the speakers.
Builds the url to grab all of the event types.
Builds the url to grab all of the languages.
totalSermonsApiRoute( $args );
Builds the url to grab the total number of sermons. Can use any of the arguments available in the getSermons method except for “pages” and “sermons_per_page”.