2012-02-25 56 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

回答