網頁

2013年10月23日 星期三

Foursquare API

FourSquare API integration with PHP website in 20 mins



Firstly we will create API key and secret for our App, for that we will go here:Foursquare Developer 

Now, we will use a PHP library which is readily available here: Foursquare Libraries 

RESTKIT的使用教程


Search Venues

https://api.foursquare.com/v2/venues/search
categoryIdasad13242l,btbe24353mA comma separated list of categories to limit results to. If you specify categoryId specifying a radius may improve results. If specifying a top-level category, all sub-categories will also match the query. Does not apply to match intent requests.
    $categoryId = '4bf58dd8d48988d12b951735';//Bus_line
    $params = array("ll"=>"$lat,$lng","categoryId"=>$categoryId); 

Foursquare Categories

找對應類別id
https://github.com/lomain/glowsquare/blob/master/categories.json


top-level category

Top Level Categories ID of Foursquare

  1. {
  2. "id": "4d4b7105d754a06374d81259",
  3. "name": "Food",
  4. "pluralName": "Food",
  5. "shortName": "Food",
  6. "icon": {},
  7. "categories": []
  8. },

Venue Categories

https://api.foursquare.com/v2/venues/categories


FourSquare venue search using categories


When I search using 5 categories:
// 4bf58dd8d48988d121941735 - lounge
// 4bf58dd8d48988d11f941735 - nightclub
// 4bf58dd8d48988d1e7931735 - jazz club
// 4bf58dd8d48988d1e9931735 - rock club
// 4bf58dd8d48988d1d8941735 - gay clubs

sub-categories
  • categories: [
    • {
      • id"4bf58dd8d48988d16d941735"
      • name"Café"
      • pluralName"Cafés"
      • shortName"Café"
      • icon: {
        • prefix"https://ss1.4sqi.net/img/categories_v2/food/cafe_"
        • suffix".png"
        }
      • primarytrue
      }
    ]

{
  "meta": {
    "code": 200
  },
  "notifications": [
    {
      "type": "notificationTray",
      "item": {
        "unreadCount": 0
      }
    }
  ],
  "response": {
    "groups": [
      {
        "type": "Recommended Places",
        "name": "recommended",
        "items": [
          {
            "reasons": {
              "count": 0,
              "items": []
            },
            "venue": {
              "id": "52b2e7af11d2a3d1701e8d0c",
              "name": "藝家書局",
              "contact": {},
              "location": {
                "lat": 25.091978,
                "lng": 121.472029,
                "distance": 46,
                "cc": "TW",
                "country": "Taiwan"
              },
   


   $url = "https://api.foursquare.com/v2/venues/explore?ll=25.0917625427,121.4716339111&radius=100&oauth_token=YOUR_KEY&v=20140327"; 

$json = file_get_contents($url);
$arr_json = json_decode($json);


$a=array();

foreach ($arr_json->response->groups[0]->items as $key => $value):
   $name = $arr_json->response->groups[0]->items[$key]->venue->name;
   $lat = $arr_json->response->groups[0]->items[$key]->venue->location->lat;
   $lng = $arr_json->response->groups[0]->items[$key]->venue->location->lng;

   $venue = array("name" => $name,"lat" =>$lat,"lng" =>$lng); 
   array_push($a,$venue );

endforeach;  


     $json_output ='{';
     $json_output .= ' "data" : ';
     $json_output .= json_encode($a) ;
     $json_output .= '}';

     echo   $json_output;

{
  "data": [
    {
      "name": "藝家書局",
      "lat": 25.091978,
      "lng": 121.472029
    },
    {
      "name": "小林紅油抄手",
      "lat": 25.091818523223,
      "lng": 121.47229998022
    }
  ]
}

Foursquare getting and displaying venues in browser

   

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。