2011-04-03 65 views
1

我想允許我的網站訪問者正常或通過Facebook註冊我的網站,並且我還希望在註冊時捕獲更多數據(例如,最喜歡的汽車)。如何使用Facebook登錄捕獲附加表單數據?

這是PHP和JavaScript代碼:

<script> 
function doSubmit() 
{ 
    var queryString = "?car="+document.forms["signup_form"]["car"].value; 
    var my_url = "http://www.mywebsite.com" + queryString; 
    var app_id = MY_ID; 
    var dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" + app_id + "&redirect_uri=" + encodeURIComponent(my_url) + "&scope=email,user_interests"; 
    top.location.href = dialog_url; 
} 
</script> 

<?php 
    $app_id = MY ID; 
    $app_secret = "MY_SECRET"; 
    $my_url = "http://www.mywebsite.com"; 
    $code = $_REQUEST["code"]; 

    if(empty($code)) { 
?> 
     <form action="go.php" method="post" id="signup_form" name="signup_form"> 
     <input id="jointext" type="text" name="name" class="auto-focus" /><br /> 
     <input id="jointext" type="text" name="email" value="" /><br /> 
     <input id="jointext" type="text" name="car" value="" /><br /> 
     <input type="submit" name="submitbutton" id="joinbutton" value="Normal Join" /> 
     <input type="button" onClick="doSubmit()" value="Facebook Join" /> 
     </form> 
<?php 
    } 
    else{ 
     $token_url = "https://graph.facebook.com/oauth/access_token?client_id=" 
      . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" 
      . $app_secret . "&code=" . $code; 

     $access_token = file_get_contents($token_url); 

     $graph_url = "https://graph.facebook.com/me?" . $access_token; 
?> 

當我測試一下「Facebook加入」按鈕,我確實看到合適的車= WhateverWasEntered在URL字符串片段,但我收到來自呼叫而產生,以獲得訪問令牌下面的錯誤?

警告:file_get_contents(https://graph.facebook.com/oauth/access ... ent_id = ...)[function.file-get-contents]:無法打開流:HTTP請求失敗! HTTP/1.0 400 Bad request in /home/index.php on line 47

Warning:file_get_contents(https://graph.facebook.com/me?)[function.file-get-contents]:無法打開流:HTTP請求失敗! HTTP/1.0 400 Bad request in /home/index.php on line 51

第47行是$ access_token = file_get_contents($ token_url); 第51行是$ user = json_decode(file_get_contents($ graph_url));

如果我更改$ my_url以包含?cars = WhateverWasEntered片段,則會顯示這些錯誤。如果我在JavaScript中添加了+ queryString,那麼一切都可以正常工作,所以我相當肯定它是傳遞表單數據或重定向URI導致問題而不是與應用程序ID或祕密相關的任何內容。

對於此代碼的任何建議,或者即使有人在使用Facebook註冊站點時,都有其他方法來捕獲其他表單數據,我們將不勝感激。

我很抱歉,如果這已被回答其他地方,我確實在四處搜尋,但沒有看到任何關於點。

感謝, 國基

+0

這聽起來像?在URL中混淆了PHP從查詢字符串中提取變量。你可以試試'print_r($ _ GET)'來看看你是否真的獲得了你期望的令牌嗎? – Lucas 2011-05-05 06:54:21

+0

使用此代替 http://developers.facebook.com/docs/plugins/registration/ – dragonjet 2011-05-07 04:23:10

回答

0

它看起來像的file_get_contents是有與HTTP文件訪問的問題。網絡文件訪問ssl可能很麻煩,如此處所述,http://php.net/manual/en/function.file-get-contents.php。你可以嘗試使用curl來獲得遠程URL,這將解決你的問題。

使用類似

$ch= curl_init($token_url); 
curl_setopt($ch, CURLOPT_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$result = curl_exec($ch); 
curl_close ($ch);