2012-02-25 58 views
1

我的问题似乎对我来说很复杂(也许不是你们),所以我会尽力解释。我正在构建一个“应用程序”(不是原生应用程序,一个基于HTML的应用程序),以便在各个场所的iPad信息亭上运行,以便人们可以在Foursquare上登记。这意味着lat/long已经是已知的(固定位置来自列表)。我已经与Facebook合作,但Foursquare正在痛苦中。Foursquare API Checkin

我已经尝试了大量不同的解决方案,教程和库(包括Epi异步库),我发现的最好的是:http://blog.jambura.com/2011/07/23/implementing-foursquare-api-with-ease/。这是我的代码基于的。

虽这么说,我的应用程序流程是这样的:

  1. 用户开始于index.php文件
  2. 用户触摸按钮“检查到Foursquare的!”
  3. 用户被路由到登录屏幕,然后到允许屏幕以允许访问应用程序。
  4. 使用来自包含文件的信息检入用户。
  5. 用户被引导回应用程序,在那里显示一个秘密词,他们可以告诉收银员打折(显然,我明白,Facebook和Foursquare都会提供签入“内置”的交易,但对于这个问题的原因只是假装这对你有意义:P)
  6. 显示该单词后,用户被自动注销,会话被销毁,应用程序移回index.php让下一个人(我不知道如何通过API注销Foursquare,但我想我会穿过那座桥,我希望session_destroy();就足够了!)

现在,重要的是要注意这一点Facebook,你可以通过PHP来做所有事情,而且根本不需要使用CURL。这允许我传递一个名为$ loc_id的GET变量,以便我有一个持久的方法来记住这个人在哪个位置,并且这允许我拉取任何“签入文本”(对于Foursquare,它也是“留言”)因为我可能需要的任何纬度/经度等。

与Foursquare,我想设置$ loc_id为会话变量,因为我必须使用POST,并且不能在我的应用程序之间携带任何基于URL的变量和Foursquare(就像我可以和Facebook一样)。

不幸的是,我只能通过Foursquare检查半路工作。让我告诉你我使用的代码(请原谅我的格式,还试图让挂起这个疯狂的系统):

  1. 的index.php<a href="checkin-foursquare.php?loc_id=<?php echo $loc_id; ?>"> < - 这是启动过程

  2. fs_conn.php

    <?PHP 
    
    // $_SESSION["loc_id"] = $_GET["loc_id"]; I removed this because I wasn't sure if it was unsetting itself all the time 
    
    $callback_url="https://www.mydomain.com/path/to/app/callback-foursquare.php"; 
    
    $client_id="xxx"; 
    $client_secret="yyy"; 
    
    $authorize_url="https://foursquare.com/oauth2/authorize?client_id={$client_id}&response_type=code&redirect_uri={$callback_url}"; 
    
    $access_token_url="https://foursquare.com/oauth2/access_token?post_checkins=post_checkins&client_id={$client_id}&client_secret={$client_secret}&grant_type=authorization_code&redirect_uri={$callback_url}&code="; 
    
    ?> 
    
  3. 签-foursquare.php

    <?php 
    
    include_once 'inc/locations.php'; // this is where my location info is stored 
    include_once 'inc/fs_conn.php'; // these are the keys/urls 
    
    session_start(); 
    
    $_SESSION["loc_id"] = $_GET["loc_id"]; 
    
    $code = $_REQUEST["code"]; 
    
    if(empty($code)) { 
        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection 
        $dialog_url = "https://foursquare.com/oauth2/authenticate?client_id=" . $client_id . "&redirect_uri=" . urlencode($authorize_url) . "&display=touch&response_type=code"; 
        echo("<script> top.location.href='" . $dialog_url . "'</script>"); 
    } else { 
    
        if($_GET['post_checkins']=='post_checkins') 
        { 
        $url = 'https://api.foursquare.com/v2/checkins/add'; 
        $fields = array(
          'venueId'=> $venue_id, 
          'venue'=> $venue_name, 
          'shout'=> $shout, 
          'broadcast'=>'public', 
          'll'=> $loc_lat . "," . $loc_long, 
          'oauth_token'=> $_SESSION['access_token'] 
         ); 
        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
        rtrim($fields_string,'&'); 
    
        $ch = curl_init(); 
        curl_setopt($ch,CURLOPT_URL,$url); 
        curl_setopt($ch,CURLOPT_POST,count($fields)); 
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
        curl_exec($ch); 
        curl_close($ch); 
        header('Location: word-foursquare.php?loc_id=' . $_SESSION["$loc_id"]); // this displays the secret word 
        exit; 
        } 
        } 
         ?> 
    
  4. 回调四角。PHP

    <?php // this is what gets redirected from Foursquare and is listed in the "callback" field 
    
    include('inc/fs_conn.php'); // keys/urls again 
    
    $access_token_url=$access_token_url.$_GET['code']; 
    $access_token=json_decode(file_get_contents("{$access_token_url}"),true); 
    $_SESSION['access_token']=$access_token['access_token']; 
    header('Location: checkin-foursquare.php?loc_id=' . $_SESSION["loc_id"] . '&post_checkins=post_checkins'); 
    exit; 
    ?> 
    

所以基本上它去的index.php - >签-foursquare.php - > foursquare.com - >回调foursquare.php - >回签,foursquare.php实际不签入 - >字foursquare.php

这是否看起来是正确的?

如果我去“https://www.mydomain.com/path/to/app/checkin-foursquare.php?loc_id=SOME-CITY-HERE”它正确加载登录页面。当我登录时,它会正确显示“连接您的foursquare帐户:”屏幕。然而,一旦我到达这一点,点击“允许”按钮指向我们回到foursquare-checkin.php,然后出现相同的“连接您的foursquare帐户:”屏幕,一遍又一遍。我觉得我缺少一些标记或某些东西,比如Foursquare没有正确授权(就像我没有提供正确的标记?),但我不知道什么是错的。 :(

我注意到在循环URL的末尾添加了'error = redirect_uri_mismatch'但我的Foursquare设置中的回调URL与fs_conn.php中的$ callback_url相同...所以我不知道这有什么错重定向URI ..?

我试图寻找这个,但它并没有太大的帮助,它看起来像我在做什么,但与代码拆分为不同的(例如,我不” t需要弹出窗口):Connecting with FourSquare API V2 using PHP

我知道我只有一半的CSRF保护在那里,但我不知道如何正确地与Foursquare实施它。这是什么导致我被困在一个循环?:(

不管怎么说,我很抱歉,这是这么长时间,我永远感激的人远远谁读过这一点。我在这里阅读了很多页面,所以这一切都开始模糊起来。我一直在使用这个网站已经有好几年了,这是我第一次从来没有能够自己想出一些东西。失败。 :(

+0

另外,我知道我没有将它设置为提前字foursquare.php ......试图让这个做登录/签入的东西先整理出来的Facebook是那么容易四方......没有那么多。!。\ – DRKMTTR 2012-02-25 13:08:53

回答