CBSSports.com Fantasy Platform Tutorial


This document will provide a simple tutorial on how to create your first application for the CBSSports.com Fantasy Platform. It will lead you step-by-step through the process to complete a sample app that will display the names of the teams in your league and their owners.


Step 1: Signing up


To create an app you first need to be a registered user of CBSSports.com and have signed up for a CBSSports.com Fantasy Platform developer account. Go to:

http://developer.cbssports.com/

and create an account via the 'Getting Started' button on the right side of the page. If you are already a CBSSports.com registered user, you can just sign in with your username and password using the boxes on the upper right. If you are already logged in, you will be identified as such in that area instead.

If you are a new user, you will need to fill out our short registration form, and provide your name, email address, date of birth and zip code. Additional information may be required from you later on if you decide to publish your app in CBSSports.com App Central, our application marketplace.

Once you have registered or signed in, you will be prompted to acknowledge and agree with our terms of service. These terms define what you can and can't do with the CBSSports.com Fantasy Platform APIs. Once you agree to this, you are ready to move to step 2.




Step 2: Creating the App


You are now signed up as a CBSSports.com Fantasy Platform developer and the Development Center will be your hub for the rest of the process. You will come here often, so be sure to bookmark it. You may have noticed that this page changed from before you were a developer. Instead of the introductory information, this page is now geared to help you develop apps.

To create an app, you will first need to let the system know what you are planning to do. You will be asked several questions, some of which you may not have the answer to right away. Don't worry, you can change your mind with many of these later on.

To start, click on the 'Create Applications' link in the top navigation. You will be presented first with a simple question asking whether you are creating an application that requires code or are you just going to be uploading content. This tutorial follows the path of creating an actual app, so select the first bullet.

This makes a number of additional fields appear. The page will look something like this:

You can get more detail on what these fields mean in the Getting Started documentation (http://developer.cbssports.com/documentation/getting-started#app_submission_and_review_process) but here's a quick guide of what to do for this example:

  • Full name: My First Sample Application
  • Short name: pick a short string of characters that pertains to your app
  • Leave the 'private' and 'baseball' buttons selected.
  • Select 'Details' as the location.

Then hit the 'Save & Continue' button.

That will bring up a second page of additional application details, but filling in that information can wait for now. Hit the 'Skip this Step' button at the top of the page. We'll come back here later.




Step 3: Create a Test League


On the Development Center Home page, you will see two boxes on the left side of the screen. The top box will list your apps. For now, it should just list the sample app you just created. The bottom box lists your test leagues. It will be empty. Let's fix that by creating a test league. Click on the 'Create Test League' button below it.

You will see a form come up. Leave the sport as 'Baseball' and the scoring type as 'Rotisserie'. Then hit 'Save & Continue'.

Now you'll be back on the Development Center Home page and the 'Test Leagues' box will now list a league which may look like this:

Don't worry about the league name, the system will randomly pick one for you. You will use this test league as an environment to get your app working. The 'Date Bump' button will come into play later on as you work on more complex applications. For now, you may ignore it. Click on the league name ('267-roto' in this case) to access the league.

This is a test league. Remember that and don't get it confused with any real leagues you play in with your friends. Once in this league you'll be able to see the app in the location that you selected in Step 2. If you followed this tutorial, it will be under Details. The Details page will look like this:

As you can see, your app now appears in the App Bar. The icon is missing because we didn't upload one yet.




Step 4: Setting up a Stub App


To move forward, you now need to create your app on your own servers. If you don't have a production-level environment, you can look into one of the many available cloud computing services. That process is beyond the scope of this document.

Create the following PHP file in your web server and call it sample.php:

<?php
echo "<html><body>";
echo "<b>THIS IS A SAMPLE FANTASY PLATFORM APP</b>";
echo "<br><br>";
echo "</body></html>";
?>

(Obviously a true application would have better HTML coding than this — but for this example, we are keeping it to the bare minimum to highlight the details relevant to building CBSSports.com Fantasy Platform Apps).

Now test it to verify that it comes up when you access it via a browser. Let's say that the URL for that page is http://myhost.mydomain.com/fantasy/sample.php. Hit that page with your browser and make sure that it works.




Step 5: Get the Access Token


In order to have access to the APIs, your application will need to pass a token on every call. This token is part of a security protocol standard called OAuth. You can read more about it on wikipedia or on many other sources on the web. The access token will be provided to your app via the iframe in which the app is embedded.

To get this token, let's first hook up your stub app to the league. Go to the Development Center Home page and click on your sample app name. The app details screen will come up. Click 'Edit'. Halfway down the page, there's a prompt for an 'iframe URL.' Enter the URL of the PHP page you created in the previous step. The 'http://' is already entered for you, so don't re-enter that. Hit the 'Save & Continue' button at the bottom of the page.

Now go back to your test league, click on Details and then click on your (missing) app icon. You should now see something like this:

Now you need to get the token. For the purpose of illustrating this process, change your application PHP file to this:

<?php
$access_token = $_GET['access_token'];
echo "<html><body>";
echo "<b>THIS IS A SAMPLE FANTASY PLATFORM APP</b>";
echo "<br><br>";
echo "token=<b>$access_token</b>";
echo "</body></html>";
?>

Then reload the application on the league page and you will have your token displayed back to you. Your page should look like this:

Obviously, you will never want to do this, as the token only works for a specific user, league and application. But now you have a method to grab the token. Note that if you run this page directly from your web server you will get an empty value for token. For testing purposes sometimes you may want to hardcode a token directly on the page and be able to test things without going thru the iframe.




Step 6: Doing Something Useful


At the beginning of this document, we said that our app would grab and display the names of the teams and owners in your league. So let's do that now.

We have two options for getting the data that we need. We can get it in XML format or JSON. Since JSON is easier to convert into internal data structures using PHP, we will use JSON.

The call to get the team names is the Fantasy Teams call, described here: http://developer.cbssports.com/documentation/api/files/teams. You can make the call from your PHP code and display the results like this:

<?php
$access_token = $_GET['access_token'];
$crl = curl_init();
$timeout = 30;
curl_setopt($crl, CURLOPT_URL,
   "http://api.cbssports.com/fantasy/league/teams?access_token=$access_token&response_format=json");
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout );
$ret = curl_exec($crl);
curl_close($crl);
$data = json_decode($ret);
echo "<html><table>";
$teams = $data->body->teams;
foreach ( $teams as $team ) {
    $name = $team->name;
    $logo = $team->logo;
    echo "<tr><td><img src='$logo'></td><td>$name</td>";
    $owners = $team->owners;
    foreach ( $owners as $owner ) {
        $ownname = $owner->name;
        echo "<td>$ownname</td>";
    }
    echo "</tr>";
}
echo "</table></html>";
?>

A couple of things here are worth clarifying. The 'curl_setopt' call is what gets the data from the fantasy teams API. The object is returned and stored in the '' variable, which is then unpacked into the '' structure. The outer loop goes through each of the teams and displays the logo and name for each team. The inner loop goes thru the owners. Remember that in CBSSports.com Fantasy, a team may have multiple owners. In this simple test league that isn't the case, but the data is always returned in an array. There's no error checking anywhere here, but a good program should always be checking for the return codes.

When accessed, the app would look like this:

The team logos appear missing because this test league doesn't have logos uploaded. But if it did, you would see them displayed there.




Step 7: Finishing Touches


You now have a functional CBSSports.com Fantasy Platform Application. It's not very compelling, but it shows how easy it is to get started. For the sake of brevity, the tutorial skipped several steps. We didn't create and upload a logo, didn't set up a description, didn't make it look nice, didn't check for errors and didn't fill out all the forms.

What we showed in this tutorial should be sufficient for you to get started playing with the APIs and learning about the capabilities of the CBSSports.com Fantasy Platform. There's a lot of documentation available on the site at http://developer.cbssports.com/documentation that you should read. Once your app is in a good-enough state to be shared with our users, whether for free or as a pay add-on, you will need to complete the Applications Details form and submit the application for approval. Our dedicated team will promptly review it, and if all goes well, turn it on and make it available for our users to install.

The potential is unlimited and we will continue to add functionality and more API calls to further improve what can be done. We hope that you find this a worthwhile effort and are able to share your experience with the rest of our users in the developer community area. And if you have suggestions for how we can improve it, we would love to hear from you.