开发者

How can I capture add'l form data with Facebook login?

I want to allow my website visitors to register for my site either normally or with Facebook and I also want to capture additional data when they sign up (e.g., favorite car).

This is the PHP and JavaScript code:

<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;
?>

When I test clicking the "Facebook Join" button, I do indeed see the appropriate ?car=WhateverWasEntered snippet in the URL string, but I receive the following errors stemming from the call to get the access token:

Warning: file_get_contents(https://graph.facebook.com/oauth/access … ent_id=...) [function.file-get-contents]: failed to open stream: HTTP request failed! 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]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/index.php on line 51

Line 47 is $access_token = file_get_contents($token_url); Line 51 is $user = json_decode(file_get_contents($graph_url));

These errors show up if I change $my_url to include the ?cars=WhateverWasEntered snippet. If I take out the +queryString addition in the JavaScript, everything works perfectly so I'm fairly certain it is the passing of form data or the redirect URI that is causing problems and not anything related to the app id or secret.

Would greatly appreciate any suggestions on this code, or even if there are alternative approaches to capture additional form data when someone is registering for a site with Facebook.

I apologize if this has been answered elsewhere, I did search around but didn't see anything on point.

Thanks, Guoji


You will want to use the new Registration plugin to accomplish this. It has a wide variety of additional fields to collect useful information.

See an example of this integrated with php at https://developers.facebook.com/docs/plugins/registration/


It looks like file_get_contents is having problems with the file access on http. Network file access to ssl can be troublesome as described here, http://php.net/manual/en/function.file-get-contents.php. You could try using curl to get the remote url instead and that will fix your problem.

Use something like

$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 ); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜