Lottery on wordpress using php and json - php

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,

Related

How to use NewsAPI in Laravel

I want to build a website and insert news articles.
I found a free newsAPI website and I got apikey as below:
https://newsapi.org/v1/articles?source=techcrunch&apiKey=3329a36068a14512b9acb66f2b8f800a
I want to my website has a Live News and display some top news. but I don't know how to write proper code in Laravel. I have tried below code and only display image. I don't know how to display author, tile, description,url, etc. like newsAPI website live response. Appreciate if someone could help me.
<?php
$urlArticles = file_get_contents('https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=3329a36068a14512b9acb66f2b8f800a');
$urlArticlesArray = json_decode($urlArticles, true);
$articles = $urlArticlesArray['articles'];
for($i = 0; $i < count($articles); $i++) {
$sites = $urlArticlesArray['articles'][$i];
echo '<img src="'.$sites['urlToImage'].'">';
}
?>
newsAPI website Live response example:
{
"status": "ok",
"source": "techcrunch",
"sortBy": "top",
-"articles": [
-{
"author": "Lucas Matney",
"title": "Gadgets",
"description": "Product reviews and demos from our TechCrunch Gadget team",
"url": "https://techcrunch.com/video/gadgets/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2013/10/techcrunch-gadgets-icon_rect.png?w=450&h=200&crop=1",
"publishedAt": "2017-04-07T23:00:45Z"
},
-{
"author": "Romain Dillet",
"title": "Uber is now banned in Italy for unfair competition",
"description": "While Uber is fighting Waymo at home, the American company is also having issues abroad. As Reuters reported, an Italian court has ordered Uber to stop all..",
"url": "https://techcrunch.com/2017/04/09/uber-is-now-banned-in-italy-for-unfair-competition/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2014/02/uber.jpg?w=764&h=400&crop=1",
"publishedAt": "2017-04-09T09:53:47Z"
},
-{
"author": "Ryan Lawler",
"title": "Gig economy stalwart TaskRabbit is contemplating a sale",
"description": "One of the earliest and most prominent startups of the so-called \"sharing economy\" or \"gig economy,\" is evaluating the possibility of selling itself. As..",
"url": "https://techcrunch.com/2017/04/08/taskrabbit-acquisition-maybe/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2015/01/taskrabbit-fire.png?w=764&h=400&crop=1",
"publishedAt": "2017-04-08T23:44:34Z"
},
-{
"author": "Ryan Lawler",
"title": "Netflix’s long-time chief product officer Neil Hunt is leaving the company",
"description": "Streaming video provider Netflix is making a change in its senior management, as the company announced long-time chief product officer Neil Hunt will be..",
"url": "https://techcrunch.com/2017/04/08/netflix-neil-hunt-leaving/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/04/los-gatos_01.jpg?w=764&h=400&crop=1",
"publishedAt": "2017-04-08T21:30:52Z"
},
-{
"author": "Natasha Lomas",
"title": "Postepic is an app for elegantly sharing book quotes",
"description": "Postepic wants to liberate all those interesting text snippets you have languishing on your camera roll and turn them into visually appealing quotations ready..",
"url": "https://techcrunch.com/2017/04/08/postepic-is-an-app-for-elegantly-sharing-book-quotes/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/04/p1050418.jpg?w=764&h=400&crop=1",
"publishedAt": "2017-04-08T16:00:31Z"
}
]
}
OK, first: I would use something like Guzzle to call external sources....
Guzzle Docs
Then, in your code...
$client = new GuzzleHttp\Client();
$url = 'https://.....';
$response = $client->get(url);
dd($response->getStatusCode()); // will dump the statuscode
dd($response->getBody()); // will dump the body
$body = $response->getBody(); //assign body to var
etc...

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

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

Graph API / FQL Does not return all events for a page

Facebook Page: http://facebook.com/getwellgabby/events/ currently has 8 events on it. I can see them. Non-admins can see them and can join them.
However, when I make calls via the Graph API or FQL, only 4 future events are returned. Results can be seen here: http://getwellgabby.org/events?raw=1 (Scroll to bottom for raw response.)
FQL Query is:
SELECT eid, name, start_time, end_time, location, venue, description
FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = 213367312037345 ) AND end_time > now()
ORDER BY end_time asc
Inspecting individual event IDs returned via Graph API shows no difference between these events. However, when editing them via the front end, they display in different dialogs.
Two sample events follow. Both were created by the same page admin approx. 24 hours apart. The first event displays properly via an API/FQL call. The second does not. From the front end, the second event displays differently than the first.
Data below was returned using the FB Graph API Expplorer Tool using an Access Token with "create_event" privileges.
Reports Correctly:
{
"id": "344143808978921",
"owner": {
"name": "Get Well Gabby",
"category": "Non-profit organization",
"id": "213367312037345"
},
"name": "Get Well Gabby Day With The Reading Phillies",
"description": "Please join [truncated...]",
"start_time": "2012-06-10T13:30:00",
"end_time": "2012-06-10T16:30:00",
"location": "FirstEnergy Stadium",
"venue": {
"street": "1900 Centre Ave.",
"city": "Reading",
"state": "Pennsylvania",
"country": "United States",
"latitude": 40.357,
"longitude": -75.91434,
"id": "223424611014786"
},
"privacy": "OPEN",
"updated_time": "2012-04-25T14:22:57+0000",
"type": "event"
}
Does not report correctly:
{
"id": "128748077259225",
"owner": {
"name": "Get Well Gabby",
"category": "Non-profit organization",
"id": "213367312037345"
},
"name": "Get Well Gabby Day With The Wilmington Blue Rocks",
"description": "Get Well Gabby Day With [truncated...]",
"start_time": "2012-07-29T13:35:00",
"end_time": "2012-07-29T16:35:00",
"timezone": "America/New_York",
"location": "Frawley Stadium",
"venue": {
"id": "148306638522325"
},
"privacy": "OPEN",
"updated_time": "2012-04-25T18:11:35+0000",
"type": "event"
}
Note, front end dialog for the event that does not report correctly will not accept additional venue information.
You can use since and until to increase the scope of your query.
here is an example using graph api explorer and search events (shows future events)
https://developers.facebook.com/tools/explorer/135669679827333/?method=GET&path=search%3Ftype%3Devent%26q%3Da%26limit%3D100%26since%3Dnow%26until%3Dnext%20year
171535666239724/events?since=2010&until=now // all events since 2010 until now
171535666239724/events?since=now&until=2013 // all events from now until 2013
for graph api requests:
until, since (a unix timestamp or any date accepted by strtotime http://php.net/manual/en/function.strtotime.php): https://graph.facebook.com/search?until=yesterday&q=orange
The issue seems to have resolved itself for now. Thanks if someone following this issue did something at Facebook.

Can we identify the date on which someone liked my page?

How can we identity the date on which someone liked my page.
is there any way where we can identify the date on which someone liked my page ?
No. You can't even get a list of people that like your page, so you can't get a date they liked it. The only information you can get is how many people like it.
You can view a chart of how many people liked your page over time at Facebook Insights.
Well no, You can make a graph call to the statuses and feeds of a user with valid access_token to get the id and name of the people who liked the post.. The timestamp can be found for the comments though ..
{
"id": "257821xxxxxxx",
"from": {
"name": "Maxxxxxx",
"id": "100xxxxxx"
},
"message": "incredible ..",
"updated_time": "2011-09-15T11:21:15+0000",
"likes": {
"data": [
{
"id": "6xxxxxx6",
"name": "Axxxxxxxxxa"
}
]
},
"comments": {
"data": [
{
"id": "257xxxxxxxxxxxx904",
"from": {
"name": "Maxxxxxxxxxxal",
"id": "1xxxxxxxxxxxxxx"
},
"message": "htxxxxxxxxxxxxxxxxxxxxxxxxxx",
"can_remove": true,
"created_time": "2011-09-15T11:22:06+0000"
}
]
}
}

Categories