用PHP四角API V2连接 [英] Connecting with FourSquare API V2 using PHP

查看:333
本文介绍了用PHP四角API V2连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的本指南得到一个快速和肮脏的OAuth认证,所以我可以使用API​​玩耍。不幸的是他的教程似乎参差不齐。引用的EpiOAuth.php文件中的链接,他找不到,我只好做一个谷歌搜索找到一个副本。当在第一时间让我的道理我在$结果= $ foursquareObj-> getAuthorizeUrl()中的index.php线收一个缺少的参数的错误运行的index.php。即使是手动放置我的回调URL中,我的令牌回来为H:

I'm using this guide to get a quick and dirty OAuth authentication so I can play around with the API. Unfortunately his tutorial seems spotty. The EpiOAuth.php file referenced wasn't found in his link and I had to do a google search to find a copy. Upon running index.php for the first time to get my token I received a 'missing parameter' error on the $results = $foursquareObj->getAuthorizeUrl() line in index.php. Even by manually placing my callback URL in, my token comes back as "h":

Login Via Foursquare
string(1) "h" 

其中,显然是不对的。为什么我(他)这样做不对?

Which, obviously, isn't right. How am I (he) doing this wrong?

推荐答案

这是不是直接回答你的问题,而是pretty直线前进获取OAuth凭证。我刚开始接触四方的,这是关于我的一切。它没有做任何错误检查,但它确实得到OAuth凭证,它允许你在API戳。

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.

本地主机/脚本/ secrets.php

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

本地主机/脚本/ 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 ) ;

本地主机/脚本/ 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>');

本地主机/ 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.

(自PHP处理与本地文件系统它实际上是preferable您secrets.php搬迁到一个位置的Web服务器路径之外以防万一。)

(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 :)

这篇关于用PHP四角API V2连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆