0

Beginner: Trying to set up a Gui for my Yahoo Sports API

I have just begun to create my fantasy sports app/website for our fantasy football league and I am looking for any advice on how to create a simple UI to pull out any of this data from this file query that I have created: http://www.bestinthemidwest.com/standings.php

If you visit the link above you will see that my code is pulling the information from yahoo. However, I am unsure of my next steps on how I should create this into a user interface? If someone could please show me a simple example of how to even pull one field into an html page I would be very grateful.

Any help that someone can give me will be much appreciated. I have a lot of web design/creation experience, but I have just begun to start investigating web SOAP / REST services.

Thanks, Boe

by
3 Replies
  • Hi Boe,

    I too am very new to all this and was stuck at the same problem as you and but after some trial and error i think i have solved it.

    First what you need to do is add

     and 

    to either end of your file like this (with your secret and your key)…

    <pre>
    <?php  
    require("OAuth.php");  
    
    $cc_key  = "YOUR KEY";  
    $cc_secret = "YOUR SECRET";  
    $url = "<http://fantasysports.yahooapis.com/fantasy/v2/game/nfl/game_weeks>";  
    $args = array();  
    $args["q"] = "yahoo";  
    $args["format"] = "json";  
    
    $consumer = new OAuthConsumer($cc_key, $cc_secret);  
    $request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);  
    $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);  
    $url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));  
    $ch = curl_init();  
    $headers = array($request->to_header());  
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
    $rsp = curl_exec($ch);  
    $results = json_decode($rsp);  
    print_r($results);  
    ?>
    </pre>
    

    What this does is format that big lump of data on you have there into a structure, you can now see the hierarchy clearly (you need that for the next bit).

    Now what i did was duplicate this file so that i still had a view of the hierarchy. On the second file i commented out the print_r($results) line and added this…

    <?php
         if(!is_null($results->fantasy_content)){  
              foreach($results->fantasy_content->game as $event){  
                   $events .= "<p>Game ID: " . $event->game_id . "<br/>Name: " . $event->name . "</p>";   
              }  
              echo $events;  
         }?>
    

    To the bottom of the file, which prints out the game_id and name elements of the feed.

    So full file looks like this…

    <pre>
    <?php  
    require("OAuth.php");  
    
    $cc_key  = "YOUR KEY";  
    $cc_secret = "YOUR SECRET";  
    $url = "<http://fantasysports.yahooapis.com/fantasy/v2/game/nfl/game_weeks>";  
    $args = array();  
    $args["q"] = "yahoo";  
    $args["format"] = "json";  
    
    $consumer = new OAuthConsumer($cc_key, $cc_secret);  
    $request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);  
    $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);  
    $url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));  
    $ch = curl_init();  
    $headers = array($request->to_header());  
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
    $rsp = curl_exec($ch);  
    $results = json_decode($rsp);  
    print_r($results);  
    ?>
    <?php
         if(!is_null($results->fantasy_content)){  
              foreach($results->fantasy_content->game as $event){  
                   $events .= "<p>Game ID: " . $event->game_id . "<br/>Name: " . $event->name . "</p>";   
              }  
              echo $events;  
         }?>
    </pre>
    

    You should be able to see from this how to display other information from different feeds, they all work in a similar format. Depending on what feed you are pulling in you will need to change

    ->game
    
    ->game_id
    
    
    and
    
    ->name

    to what you need to pull out

    You are not up to as far as i have worked out :)

    This is like not the correct way of doing this, but it worked for me. Happy to be told where i have went wrong!

    Hope this helps.

    Paul

    0
  • Sorry second paragraph should read…

    First what you need to do is add

    <pre>…</pre>
    

    to either end of your file like this (with your secret and your key)…

    0
  • Paul,

    Thank you so much for the reply. I have tried to implement what you explained above but I am still running into issues.I will try to give you a very detailed explanation of what I am doing to see if you can tell me where I am going wrong.

    First off I do have an OAuth.php file uploaded to the root of bestinthemidwest.com.

    After I verified that I had the OAuth.php file uploaded, then I uploaded the below code on my server: http://www.bestinthemidwest.com/standings.php

    <pre>
    <?php
    require("OAuth.php");
    
    $cc_key  = "I inserted my on key";
    $cc_secret = "I also inserted my own secret";
    $url = "<http://fantasysports.yahooapis.com/fantasy/v2/game/nfl>";
    $args = array();
    $args["q"] = "yahoo";
    $args["format"] = "json";
    
    $consumer = new OAuthConsumer($cc_key, $cc_secret);
    $request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);
    $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
    $url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
    $ch = curl_init();
    $headers = array($request->to_header());
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $rsp = curl_exec($ch);
    $results = json_decode($rsp);
    print_r($results);
    ?>
    <?php
         if(!is_null($results->fantasy_content)){  
              foreach($results->fantasy_content->game as $event){  
                   $events .= "<p>Game ID: " . $event->game_id . "<br/>Name: " . $event->name . "</p>";   
              }  
              echo $events;  
         }?>
    </pre>
    

    Then here is a screenshot to the response that I got in the browser: http://www.bestinthemidwest.com/yahoo/image.png

    Then I realized I needed to enter something for the "game", "game_id", and "name" in the code, so I entered this code into my php file.

    <pre>
    <?php
    require("OAuth.php");
    
    $cc_key  = "I entered my key";
    $cc_secret = "I also entered my secret";
    $url = "<http://fantasysports.yahooapis.com/fantasy/v2/game/nfl>";
    $args = array();
    $args["q"] = "yahoo";
    $args["format"] = "json";
    
    $consumer = new OAuthConsumer($cc_key, $cc_secret);
    $request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);
    $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
    $url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
    $ch = curl_init();
    $headers = array($request->to_header());
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $rsp = curl_exec($ch);
    $results = json_decode($rsp);
    print_r($results);
    ?>
    <?php
         if(!is_null($results->fantasy_content)){  
              foreach($results->fantasy_content->nfl as $event){  
                   $events .= "<p>Game ID: " . $event->273 . "<br/>Name: " . $event->Football . "</p>";   
              }  
              echo $events;  
         }?>
    </pre>
    

    Then, the following link will show you the error I got when this file was uploaded to the server: http://www.bestinthemidwest.com/yahoo/image2.png

    I am guessing that I am putting the

    ->game

    ->game_id

    and

    ->name

    fields wrong but I am I am not exactly sure how to enter them in correctly.

    Thanks in advance for your feedback!

    Boe Barlage

    0

Recent Posts

in Fantasy Sports API