Created
May 17, 2016 01:35
-
-
Save jimmycann/8025c0e59387315a5349eb2b271e464b to your computer and use it in GitHub Desktop.
PHP - Retrieve data from the Foursquare venues API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Foursquare Venue Search API Sample | |
//API Documentation available at https://developer.foursquare.com/docs/venues/search | |
//Retrieve data sent from your application | |
$postdata = file_get_contents("php://input"); | |
$data = json_decode($postdata); | |
//This connects to AngularJS, however depending on your application you ay need to retrieve from $_POST | |
//Required parameter | |
//Latitude | |
//Longitude | |
//Limit (max is 50 per request) | |
//Optional param | |
//Offset - For pagination of results | |
$foursquare = get_foursquare_data($data); | |
echo $foursquare; | |
function get_foursquare_data($data) | |
{ | |
$secret = FOURSQUARE_SECRET; | |
$cid = FOURSQUARE_CLIENTID; | |
$host = "https://api.foursquare.com/v2/venues/"; | |
$ver = '20160510'; | |
$limit = $data->limit; | |
$offset = ""; | |
if ($data->offset > 0) { | |
$offset = "&offset=" . $data->offset; | |
} | |
$url = $host . "search?ll=" . $data->lat . "," . $data->long . "&limit=" . $limit . $offset . "&client_id=" . $cid . "&client_secret=" . $secret . "&v=" . $ver; | |
//Final URL Example | |
//https://api.foursquare.com/v2/venues/search?ll=37.8141,144.9633&limit=1&offset=1&client_id=FOURSQUARE_CLIENTID&client_secret=FOURSQUARE_SECRET&v=20160510 | |
// initiate curl | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
//Response Example | |
// { | |
// meta: { | |
// code: 200 | |
// requestId: "573a747f498e7c431ab277e3" | |
// } | |
// notifications: [ | |
// { | |
// type: "notificationTray" | |
// item: { | |
// unreadCount: 0 | |
// } | |
// } | |
// ] | |
// response: { | |
// venues: [ | |
// { | |
// id: "513a6079e4b06b29f6ec4dbe" | |
// name: "Melbourne Exhibition Centre" | |
// contact: { } | |
// location: { | |
// lat: 37.82331085205078 | |
// lng: 144.95330810546875 | |
// distance: 1350 | |
// cc: "XX" | |
// } | |
// categories: [ | |
// { | |
// id: "4bf58dd8d48988d1ff931735" | |
// name: "Convention Center" | |
// pluralName: "Convention Centers" | |
// shortName: "Convention Center" | |
// icon: { | |
// prefix: "https://ss3.4sqi.net/img/categories_v2/building/conventioncenter_" | |
// suffix: ".png" | |
// } | |
// primary: true | |
// } | |
// ] | |
// verified: false | |
// stats: { | |
// checkinsCount: 3 | |
// usersCount: 3 | |
// tipCount: 0 | |
// } | |
// specials: { | |
// count: 0 | |
// items: [ ] | |
// } | |
// hereNow: { | |
// count: 0 | |
// summary: "Nobody here" | |
// groups: [ ] | |
// } | |
// referralId: "v-1463448703" | |
// venueChains: [ ] | |
// } | |
// ] | |
// confident: false | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment