drstumptown Posted March 11, 2014 Share Posted March 11, 2014 (edited) 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 March 11, 2014 by drstumptown Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/ Share on other sites More sharing options...
jairathnem Posted March 11, 2014 Share Posted March 11, 2014 That is JSON output. use json_decode to get the data into an array. sample: $data = json_decode($response); $data is an array containing values of $response. Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472114 Share on other sites More sharing options...
drstumptown Posted March 11, 2014 Author Share Posted March 11, 2014 (edited) Ah - ha!!!!! so your telling me to replace: echo $response; with $data = json_decode($response); and then Mai Thais and yachts?! ah, spoke to soon; white screen of death occurs... Edited March 11, 2014 by drstumptown Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472120 Share on other sites More sharing options...
jairathnem Posted March 11, 2014 Share Posted March 11, 2014 (edited) 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 March 11, 2014 by jairathnem Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472122 Share on other sites More sharing options...
drstumptown Posted March 11, 2014 Author Share Posted March 11, 2014 (edited) 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 March 11, 2014 by drstumptown Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472189 Share on other sites More sharing options...
drstumptown Posted March 11, 2014 Author Share Posted March 11, 2014 (edited) 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 March 11, 2014 by drstumptown Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472193 Share on other sites More sharing options...
jairathnem Posted March 11, 2014 Share Posted March 11, 2014 $response = file_get_contents('http://api.nfldata.apiphany.com/developer/JSON/NewsByTeam/ARI?'.$getdata, false, $context); $data = json_decode($response,true); JSON is added to the data as an array. if you want to access the 'Content' field, use $data['Content'] Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472196 Share on other sites More sharing options...
drstumptown Posted March 11, 2014 Author Share Posted March 11, 2014 (edited) 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 March 11, 2014 by drstumptown Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472198 Share on other sites More sharing options...
jairathnem Posted March 11, 2014 Share Posted March 11, 2014 you missed the important line - $data = json_decode($response,true); check my previous post! Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472201 Share on other sites More sharing options...
drstumptown Posted March 11, 2014 Author Share Posted March 11, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472202 Share on other sites More sharing options...
jairathnem Posted March 11, 2014 Share Posted March 11, 2014 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']; Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472204 Share on other sites More sharing options...
drstumptown Posted March 11, 2014 Author Share Posted March 11, 2014 (edited) 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 March 11, 2014 by drstumptown Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472207 Share on other sites More sharing options...
jairathnem Posted March 11, 2014 Share Posted March 11, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472210 Share on other sites More sharing options...
drstumptown Posted March 11, 2014 Author Share Posted March 11, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472212 Share on other sites More sharing options...
.josh Posted March 11, 2014 Share Posted March 11, 2014 Try echo $data[0]['content']; Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472215 Share on other sites More sharing options...
.josh Posted March 11, 2014 Share Posted March 11, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472218 Share on other sites More sharing options...
drstumptown Posted March 11, 2014 Author Share Posted March 11, 2014 wow... Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472221 Share on other sites More sharing options...
.josh Posted March 11, 2014 Share Posted March 11, 2014 yep.. wow indeed. p.s. I typo'd. content != Content. Hopefully you caught that from your pro debugging skillz. Quote Link to comment https://forums.phpfreaks.com/topic/286874-parsing-data-from-api-with-php/#findComment-1472225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.