Connecting with FourSquare API V2 using PHP

房东的猫 提交于 2019-11-29 05:11:44

This isn't a direct answer to your question, but is pretty straight forward for getting an OAuth token. I'm just getting started with foursquare, and this is about all I've got. It does not do any kind of error checking, but it does get an OAuth token which allows you to poke at the API.

localhost/scripts/secrets.php

<?php  
  // insert your foursquare API keys
  define('CLIENT_ID', 'YOUR_CLIENT_ID');
  define('CLIENT_SECRET', 'YOUR_CLIENT_SECRET');

localhost/scripts/4sq_Login.php

<?php
// Foursquare login stage 1, build url and redirect
  require_once('secrets.php'); //defines CLIENT_ID

// build $url
  $url = 'https://foursquare.com/oauth2/authenticate';
  $url .= '?client_id='.CLIENT_ID;
  $url .= '&response_type=code';
  $url .= '&redirect_uri=http://localhost/scripts/4sq_Callback.php'; // change to your 4sq callback

// redirect
  header( 'Location: '.$url ) ;

localhost/scripts/4sq_Callback.php

<?php
// Foursquare login step 2, echo back $code from QUERY_STRING
  require_once('secrets.php'); // defines CLIENT_ID & CLIENT_SECRET

// get $code from QUERY_STRING
  parse_str($_SERVER['QUERY_STRING'], $query);
  $code = $query['code'];

// build url
  $url = 'https://foursquare.com/oauth2/access_token';
  $url .= '?client_id='.CLIENT_ID;
  $url .= '&client_secret='.CLIENT_SECRET;
  $url .= '&grant_type=authorization_code';
  $url .= '&redirect_uri=http://localhost/scripts/4sq_Callback.php'; //change to your 4sq callback
  $url .= '&code='.$code;

// call to https://foursquare.com/oauth2/access_token with $code
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_URL, $url);
  $result = curl_exec($ch);
  curl_close($ch);

// $result value is json {access_token: ACCESS_TOKEN}
  $values = json_decode($result, true);
  $token = $values['access_token'];

// set access_token cookie (if you wish)
  $expire = time()+2592000; // 30 days from now
  setcookie("foursquare_token", $token, $expire, '/');

// crosswindow scripting to pass back $token
  echo('<script type="text/javascript">');
  echo('opener.set4sqKey("'.$token.'");');
  echo('self.close();'); // close self
  echo('</script>');

localhost/index.htm

<!DOCTYPE HTML>
<html>
<head>
<title>FourSquare test page...</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
<!--
var foursquareKey;

// Open foursquare login window.
function get4sqKey(){
  if(!foursquareKey){
    window.open('scripts/4sq_Login.php', 'foursquareAuth', 'width=960, height=548');
  }
}

// called crosswindow by login window
function set4sqKey(key){
  foursquareKey = key;
  setTimeout('alert("Logged into Foursquare");', 1); // setTimeout makes alert non-blocking
}

// simple alert to display OAuth token
function showKey(){
  alert(foursquareKey);
}

// -->
</script>
</head>
<body>
<a href="javascript:get4sqKey();">get4sqKey();</a> |
<a href="javascript:showKey();">showKey();</a>
</body>
</html>

I'm not good at explaining, so I hope you can see what I'm doing, and can build on it as necessary.

(Since PHP deals with the local file system it is actually preferable to relocate your secrets.php to a location outside of the web server path. Just in case :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!