Jump to content

parsing data from API with PHP


drstumptown

Recommended Posts

Hello all,

 

Fairly newb when it comes to this kind of PHP; OK, so I am using NFL Data API https://developer.nfldata.com/docs/services/231 to create a small web app. Below I'm pulling the data from the API but it outputs all over the place with way extra sytnax and attributes.

<?php    

$query_params = array(
    // Specify your developer key
    'key' => 'XXXValidDeveloperKey',
);     

$getdata = http_build_query($query_params);    

$opts = array('http' =>
 array(
    'method'  => 'GET',
    'content' => $getdata
    )
);    

$context  = stream_context_create($opts);    

$response = file_get_contents('http://api.nfldata.apiphany.com/developer/JSON/NewsByTeam/ARI?'.$getdata, false, $context);
echo $response;

?>

Example of output.

 

[{"Content":"The Cardinals have offered free agent ILB Karlos Dansby a two-year deal worth $10 million to $12 million.","NewsID":9701,"PlayerID":11658,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\".

 

Wondering how to filter the data to pull out nice and formatted using PHP? And then MAYBE, just MAYBE, I can grab specific pieces of it; eg: NewsID etc.

 

Thanks for any assistance!!

Edited by drstumptown
Link to comment
Share on other sites

From the example output you posted it looks like the json isnt well formed.

 

The output should be something similar to the below:

[{"Content":"The Cardinals have offered free agent ILB Karlos Dansby a two-year deal worth $10 million to $12 million.","NewsID":9701,"PlayerID":11658,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\""}]

missing a couple of characters at the end.

 

echo $response and post it here.

 

 

if it was correctly formed, when you var_dump $data it should output

array (size=1)
0 =>
object(stdClass)[1]
public 'Content' => string 'The Cardinals have offered free agent ILB Karlos Dansby a two-year deal worth $10 million to $12 million.' (length=105)
public 'NewsID' => int 9701
public 'PlayerID' => int 11658
public 'Source' => string 'NBCSports.com' (length=13)
public 'Team' => string 'ARI' (length=3)
public 'TermsOfUse' => string 'NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: "NBCSports.com"' (length=280)
Edited by jairathnem
Link to comment
Share on other sites

Hey, thanks a bunch for the responses; I'm confused with your last suggestion. Did I replace the snippet incorrectly? Yes, I know it's outputting in bad format that's why I'm here. Basically I'm trying to figure out how to grab, format, filter the data. If JSON, then from PHP > JSON I suppose. When I echo it, previous to your snippet suggestion; it just plasters miles of data like: (I want to grab say 'Source' or etc and format?)

 

 

[{"Content":"The Cardinals have offered free agent ILB Karlos Dansby a two-year deal worth $10 million to $12 million.","NewsID":9701,"PlayerID":11658,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". etc etc

 

Maybe:

file_get_contents

Isn't the best approach?

Edited by drstumptown
Link to comment
Share on other sites

Here is the full output though; (below)

 

I want to grab it with PHP I guess as I did; but whatever I need to do with JSON to be able to use it. Really appreciate the help!

 

 

[{"Content":"The Cardinals have offered free agent ILB Karlos Dansby a two-year deal worth $10 million to $12 million.","NewsID":9701,"PlayerID":11658,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Cardinals make Dansby 2-year, $10-12M offer","Updated":"\/Date(1394399340000-0400)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/2663\/karlos-dansby"},{"Content":"Cardinals signed TE John Carlson to a two-year, $4.65 million contract.","NewsID":9679,"PlayerID":null,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Cards sign TE Carlson to 2-year, $4.65M deal","Updated":"\/Date(1394226180000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/4674\/john-carlson"},{"Content":"In a post he wrote for the Huffington Post, free agent Rashard Mendenhall hinted he could retire.","NewsID":9575,"PlayerID":3931,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Free agent RB Mendenhall hints he may retire","Updated":"\/Date(1393355400000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/4652\/rashard-mendenhall"},{"Content":"Speaking at the Combine on Friday, coach Bruce Arians said the Cardinals "want to build our offense around" Andre Ellington.","NewsID":9539,"PlayerID":15224,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Cardinals to 'build offense around' Ellington","Updated":"\/Date(1392998880000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/8388\/andre-ellington"},{"Content":"GM Steve Keim reiterated that the Cardinals will use multiple backs going forward.","NewsID":9524,"PlayerID":15224,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"GM Keim says Cardinals will use multiple RBs","Updated":"\/Date(1392914220000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/8388\/andre-ellington"},{"Content":"According to "two NFL sources," Larry Fitzgerald has agreed only to a "simple restructuring," and not a pay cut.","NewsID":9452,"PlayerID":5571,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Report: Fitz agrees to 'simple restructuring'","Updated":"\/Date(1391556720000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/1661\/larry-fitzgerald"},{"Content":"Larry Fitzgerald has agreed to a restructured contract.","NewsID":9451,"PlayerID":5571,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Fitzgerald agrees to restructured contract","Updated":"\/Date(1391551320000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/1661\/larry-fitzgerald"},{"Content":"Larry Fitzgerald says he's "absolutely" willing to restructure his contract.","NewsID":9421,"PlayerID":5571,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Fitz 'absolutely' willing to redo contract","Updated":"\/Date(1391106660000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/1661\/larry-fitzgerald"},{"Content":"According to an "individual" CSN New England's Tom Curran believes "without reservation," the Patriots were interested in acquiring Larry Fitzgerald last offseason.","NewsID":9408,"PlayerID":5571,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Report: Pats were interested in Fitz in 2013","Updated":"\/Date(1390943760000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/1661\/larry-fitzgerald"},{"Content":"Cardinals DB Tyrann Mathieu (torn ACL, LCL) is expected to require a six-to-nine month recovery timetable.","NewsID":9310,"PlayerID":15226,"Source":"NBCSports.com","Team":"ARI","TermsOfUse":"NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. Attribution to NBCSports.com must be provided in connection with your use of the feeds. If you provide this attribution in text, please use: \"NBCSports.com\". If you provide this attribution with a graphic, please use the NBCSports.com logo that we have included in the feed itself. We reserve all rights in and to the NBCSports.com logo, and your right to use the NBCSports.com logo is limited to providing attribution in connection with these RSS feeds. By using the NBCSports.com RSS news feed, you agree to be bound by the NBCSports.com Terms and Conditions. NBCSports.com reserves the right to modify these Terms of Use and the Terms of Service referenced above from time to time and without notice. Msnbc.com reserves the right to cease offering these feeds at any time or require you to cease distributing these feeds at any time for any reason.","Title":"Tyrann Mathieu (knee) has 6-9 month timetable","Updated":"\/Date(1389913800000-0500)\/","Url":"http:\/\/www.rotoworld.com\/player\/nfl\/8387\/tyrann-mathieu"}]

Edited by drstumptown
Link to comment
Share on other sites

Ah ha -

 

so:

$response = file_get_contents('http://api.nfldata.apiphany.com/developer/JSON/NewsByTeam/ARI?'.$getdata, false, $context);

$data['Content']; /* or interchanged with the piece of data I want ['NewsID'] ['PlayerId'] etc */

 Actually this resulted in the blank white screen again.

Edited by drstumptown
Link to comment
Share on other sites

Thank you for your candor, but...

<?php    

$query_params = array(
    // Specify your developer key
    'key' => 'XXXValidDeveloperKey',
);     

$getdata = http_build_query($query_params);    

$opts = array('http' =>
 array(
    'method'  => 'GET',
    'content' => $getdata
    )
);    

$context  = stream_context_create($opts);    

$response = file_get_contents('http://api.nfldata.apiphany.com/developer/JSON/NewsByTeam/ARI?'.$getdata, false, $context);
$data = json_decode($response,true);
$data['Content'];
?>

is still white screening it.

Link to comment
Share on other sites

you gotta learn the basics first.

 

just typing $data['content'] won't show anything.

$response = file_get_contents('http://api.nfldata.apiphany.com/developer/JSON/NewsByTeam/ARI?'.$getdata, false, $context);
$data = json_decode($response,true);
echo $data['Content'];
Link to comment
Share on other sites

didn't work, dude. Still blank..

<?php    

$query_params = array(
    // Specify your developer key
    'key' => 'XXXDEVELOPERKEY',
);     

$getdata = http_build_query($query_params);    

$opts = array('http' =>
 array(
    'method'  => 'GET',
    'content' => $getdata
    )
);    

$context  = stream_context_create($opts);    

$response = file_get_contents('http://api.nfldata.apiphany.com/developer/JSON/NewsByTeam/ARI?'.$getdata, false, $context);
$data = json_decode($response,true);
echo $data['Content'];
?>
Edited by drstumptown
Link to comment
Share on other sites

echo $response and see if it is holding value.

$response = file_get_contents('http://api.nfldata.apiphany.com/developer/JSON/NewsByTeam/ARI?'.$getdata, false, $context);
echo $response;
$data = json_decode($response,true);
echo $data['Content'];

if it is printing, next check $data else issue with the api.

$response = file_get_contents('http://api.nfldata.apiphany.com/developer/JSON/NewsByTeam/ARI?'.$getdata, false, $context);
$data = json_decode($response,true);
var_dump($data);
echo $data['Content'];

if it did check the structure of the printed array.

 

these are basic triage methods.

Link to comment
Share on other sites

I appreciate your feedback and the semantics -- but my question still persists. Your option 1, above printed exactly as I had it in the beginning; and your second set of snippet print, including this type of data (below)

array(10) { [0]=> array(9) { ["Content"]=> string(88)

I understand this is a multistage question; but still I'm just trying to grab the data to use it, format / filter per; like you mentioned to get ['content']. Yes, I also understand this isn't senior PHP, thanks again for the responses.

Link to comment
Share on other sites

Also, I understand it's off-putting to hear stuff like "this is php 101 basic shit" but it's also off-putting on the helper's end when someone is struggling with how to do something because they won't take the time to first learn the "php 101 basic shit".

 

If you really want to learn how to code, take a step back and learn the basic syntax rules, how to echo things out, array structures, methods for debugging when things don't work as expected, etc.. because all of these issues you have posted have basically amounted to you not understanding php 101.

 

And that's the point.. you aren't posting to learn php 101. You're asking for help on how to solve an issue despite you not learning the basics. We aren't really here to write code for people. Nor are we here to even handhold people through coding issues. This is a learning site. We expect people to learn things and if they get stuck, push them in the right direction.

 

So, when you got stuck and post an issue like this and basically say "it doesn't work" and have taken no steps to figure out what the problem is, that's when we tell you to step back and learn how to figure out what/where the problem is (debugging). Not because we're being elitist pretentious douchebags pointing at the noobie, but because teaching you how to solve your own problem is more valuable than solving your problem for you.

 

I'm sorry if that's not what you wanted to hear, but as said, this is a learning site, not a "help you bluff your way through a problem" or "do your work for you" site. If you aren't really interested in learning to code, then I suggest you hire a freelancer to do it for you, or hit up one of the rep-whore sites like Stack Overflow where people will be more than happy to show you the answer no-questions-asked for an upvote.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.