john handcock john handcock - 1 year ago 39
JSON Question

ajax call that returns json file

im trying to figure out what the easiest way would be. i have a to do an api call for data i get a json file in return but im trying to parse the data to php so that my website would just pull from the database. do i have to create all the same fields that the api call is using for instance name gender age height. and then do i name it something else so i can call from my webpage. because in order to get a nfl player it gives me a 16 digit code and i want it just to be say tom brady

i have used javascript to pull the data but dont know what to do from there

"players": [{
"name": "Kyle Rudolph",
"jersey": "82",
"last_name": "Rudolph",
"first_name": "Kyle",
"abbr_name": "K.Rudolph",
"preferred_name": "Kyle",
"birth_date": "1989-11-09",
"weight": 265.0,
"height": 78,
"status": "A01",
"id": "1059e9dc-97df-4643-9116-883a0573d8b1",
"position": "TE",
"birth_place": "Cincinnati, OH, USA",
"high_school": "Elder (OH)",
"college": "Notre Dame",
"college_conf": "Independent",
"rookie_year": 2011,
"draft": {
"year": 2011,
"round": 2,
"number": 43,
"team": {
"name": "Vikings",
"market": "Minnesota",
"alias": "MIN",
"id": "33405046-04ee-4058-a950-d606f8c30852"

Answer Source

John, you don't have to use the same names. Once your PHP receives data from the API, parse it into whatever names you want, ignore the values you don't need etc... For example, suppose the API gives you a first and last name, but in your database you only care about full name. As another example, if the API gives you player weight in pounds but you need it in kilograms:

$api_result = file_get_contents($url);
$api_data = json_decode($api_result);
$name = "$api_data->last, $api_data->first"
$weight = $api_data->weight * 0.454; //convert pounds to kg for storage

Now you can store $name and $weight in your DB as you like. When your website pulls data from your backend, the shape of the data produced by the API doesn't matter because you stored it in the shape that is most helpful to your application