ajax call that returns json file - php

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"
}
},

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

Related

Laravel - Eloquent to Json, and then sortBy on json object not working

I have this json value that I want to be sorty but for some reason it's not working.
[
{
"id": 15028,
"order_id": 342,
"user_id": 3,
"status": "1",
"priority": "1",
"donedate": null,
"user": {
"id": 3,
"name": "Max"
}
},
{
"id": 15030,
"order_id": 341,
"user_id": 4,
"status": "2",
"priority": "1",
"donedate": null,
"user": {
"id": 4,
"name": "Jon"
}
}
]
This jSon structure is the result of Laravel eloquent object conversion using $object->toJson();
Now I keep this output in my Redis cache. What I want is to when the status and or priority of any order gets changed then I want to sort this jSon and store it back in Redis.
$order_list = collect($json_decoded_with_updated_values);
$order_list = $order_list->sortBy('status')->sortBy('priority');
Redis::set(\GuzzleHttp\json_encode($stich_list_in_collection));
Redis::set("orders_list", $orders_list, 302400);
However, I don't get a sort list. What I want to achieve is that, just like I would run two to three orderBy on an eloquent model like orderBy('status')->orderBy('priority')->get() .. I want to run the same two sortings on this json list.
Thanks in advance.
I figured it out. Actually we don't need to have a call-back as suggested by #brokedid. We can do it like following.
$order_list->sortBy('status')->sortBy('priority')->values()->all();
So I was missing the "->values()->all()" part. I hope if any one runs into the same problem in future, they can get a hint from this.
If you want to sort by multiple Fields, then you could try to sort with a callback-method:
$orderedList = $unorderedList->sortBy(function($item) {
return $item->priority.'-'.$item->status;
});
I wonder what's the result when you choose a different sort direction.
$order_list = $order_list->sortByDesc('status');

HERE Api: Using Positioning APIto get Lat and Long

I'm trying to use the Using Positioning API to search for the latitude and longitude of a given point, however I only have MCC, MNC, TimeAdvance, LAC, Cell_ID, and Signal Power. How can I use this data to fetch the location?
I tried to use as below but did not return any data.
{
"gsm": [
{
"mcc": 724,
"mnc": 10,
"lac": 2397,
"cid": 453,
"nmr": [{ "bsic": "0", "bcch": "0" }]
},
{
"mcc": 724,
"mnc": 10,
"lac": 2397,
"cid": 3045,
"nmr": [{ "bsic": "0", "bcch": "0" }]
}
]
}
Im using cURL.
Thanks
This response means that you have sent a valid request for which the API cannot determine a location.
I have tried to find the cell towers in your request using various cell tower databases and I could not find them anywhere. If they do exist they are most likely not in our (or any) database at this point.

Lottery on wordpress using php and json

I am currently building a website for my guild (on a video game).
The video game (Guild wars 2) provide us a feed of everything being done inside the guild in a json file (user joining, withdraw of money, deposit of items, kick, etc...).
Since I have this feed, auto-updated, I would like to setup a lottery, working on the following :
- guild member (user) buy a ticket => deposit golds in our bank (displayed as deposit)
- display this information on the website => user "x" has bought a ticket at H:M:DD:MM:Y (for example)
- at the end of the month, script to random a winner from this list of users (this random must include only tickets bought between date A & B)
I have managed to write a little code, and it is working, only to display a list of users and their actions :
<?php
$apikey = '(My API key)';
$guildkey = '(My guild key)';
$jsontest =file_get_contents ('https://api.guildwars2.com/v2/guild/'.$guildkey.'/log?access_token='. $apikey);
$json = json_decode($jsontest,true);
foreach($json as $member)
{
if ($member['coins'] > 1)
print "Guild member "."".$member['user'].""." has bought a ticket for ".$member['coins']." golds"." ";
}
?>
Here a little example of how the guild wars 2 api output is :
{
"type": "upgrade",
"item_id": 77729,
"count": 1,
"action": "completed",
"id": 896815,
"time": "2017-02-23T14:26:29.000Z",
"user": "Lehi.5091",
"upgrade_id": 668
},
{
"id": 896814,
"time": "2017-02-23T14:26:29.000Z",
"type": "upgrade",
"upgrade_id": 668,
"action": "queued"
},
{
"type": "upgrade",
"item_id": 77729,
"count": 1,
"action": "completed",
"id": 896810,
"time": "2017-02-23T14:26:28.000Z",
"user": "Lehi.5091",
"upgrade_id": 668
As intended, it does display what I want, here the working code
If you have tips or anything about how to this, I'll gladly take them.
Thanks for your help,

Update multiple mysql tables with one json in php

I've got an AngularJS frontend and got some problems relating the data layer.
In the frontend I've got contacts.
contact JSON object:
{
"id": 1,
"groupId": 1,
"firstName": "John",
"lastName": "Doe",
"email": "jd#gmail.com",
"company": "Google",
"info": "lorem ipsum dolor amet",
"numbers": [
{
"typeId": 1,
"number": "123456"
},
{
"typeId": 2,
"number": "1234567"
},
{
"typeId": 3,
"number": "8765432"
},
{
"typeId": 4,
"number": "0864235"
}
]
}
I get this object over an url with one http request.
When I will save it I give exactly this object back to the backend.
But how can I e.g. update a number out of the numbers table or the company out of the companies table with php?
My colleague said to me that when i update a number in the contact object, i have to call an url with /numbers/id .
But I just want to put back a "contact object" at /contacts
and let the business layer/data layer do the logic and split the object to update different tables.
In the backend he uses php and fat-free-framework.
Can anyone explain how to do this?
-EDIT-
And I thought the complete logic how to save the data is in the backend.
With DTO (Data Transfer Object) and DAO (Data Access Object) etc.
If you want to accept an object graph then you would need to parse it then make the appropriate calls to the database.
For example, presuming you have a table "numbers" that has columns "typeId" and "number" you would probably want logic in your "contact" class that does something like...
// convert the JSON to PHP
$request = json_decode($contact);
if(isset($request->numbers)) {
$number = new \DB\SQL\Mapper($this->db, "number");
// loop over your number objects
foreach($request->numbers as $data) {
// create a new number entry for each one
$number->copyfrom($data);
$number->save();
$number->reset();
}
}
Obviously how and where you do this would depend on how you have structured your code, for example contact->post() and contact->put() might be applicable places.

How do i show the maps for the venue while creating an event programmatically with the PHP SDK?

When i create an event with the graph api i need to specify the venue and i would like also to show the map.
I do
$fb_event['name'] = "THis is to test latitude";
$fb_event['description'] = "And longitude!!!!";
$fb_event['start_time'] = date( "c", Ai1ec_Facebook_Event::get_facebook_start_time($event->start));
$fb_event['location'] = "Where you want";
$fb_event['street'] = "Via andrea del sarto 9";
$fb_event['city'] = "Milan";
$fb_event['latitude'] = 45.444975793404;
$fb_event['longitude'] = 9.2119209654715;
$facebook = $this->facebook_instance_factory();
try {
$result = $facebook->api( "/me/events", "POST", $fb_event );
} catch (FacebookApiException $e) {
fb($e);
}
This produce this event which show the correct street and city, but no map. If i edit the event and save, the map "Magically" appears using the street and city correctly.
In any case latitude and longitude are ignored.
What am i doing wrong?
If you compare your two test events in the Graph API explorer you'll see that Event ID 239298922846828 does not have its latitude and longitude populated, while 245655182207213 does. I'm assuming 245655182207213 is an event you've edited.
Looking at your code, you seem to be doing everything as described in the documentation. However, I've found that what is described does not always work.
What I've been seeing is that events populated from within Facebook that occur at a known venue are no longer allowing you to specify an address. Instead all they save is a venue id within Facebook which you can then drill into to get the address, etc.
Take a look at one of my events. For this event, there is no way to edit the details of this location from within Facebook, nor does the event venue details get returned with an API call. I'm using the API to pull the event details to an external website. This change caused me days of frustration.
I started seeing this behavior in late April. I haven't found any official documentation announcing this change.
When some documentation appears, what I expect the new event venue workflow will be is something like:
Query the area where your event will take place to see if a venue exists already.
If yes, get save its id.
If not, create a new community page for your venue and save its id.
Use this ID to populate the event venue.
In the end this is a known facebook bug
https://developers.facebook.com/bugs/173095916131752
Two different formats when create using the same parameters.
When create event through gql
{
"id": "xxxxxxxxxxxxxx",
"owner": {
"name": "xxxxxxxxxx",
"id": "xxxxxxxxx"
},
"name": "W1112",
"start_time": "2013-10-22",
"is_date_only": true,
"location": "Tulsa, OK, United States",
"venue": {
"latitude": 36.131388888889,
"longitude": -95.937222222222,
"street": "",
"zip": "",
"id": "109436565740998"
},
"privacy": "SECRET",
"updated_time": "2013-09-19T12:23:26+0000"
}
When create event through fb
{
"id": "xxxxxxxxxxxxxx",
"owner": {
"name": "xxxxxx",
"id": "xxxxxxxx"
},
"name": "1234",
"start_time": "2013-10-09T21:26:00+1100",
"end_time": "2013-10-25T00:26:00+1100",
"timezone": "Australia/Sydney",
"is_date_only": false,
"location": "Maroubra Junction",
"venue": {
"latitude": -33.940804216453,
"longitude": 151.23876752992,
"city": "Maroubra",
"state": "NSW",
"country": "Australia",
"id": "153993547968514",
"street": "832 anzac Parade ",
"zip": "2035"
},
"privacy": "SECRET",
"updated_time": "2013-09-19T10:50:14+0000"
}

Categories