FB登录集成在网站上

时间:2014-08-20 05:36:42

标签: php facebook facebook-php-sdk facebook-login

我正在尝试为网站整合FB登录。我现在能够登录但是现在我无法理解如何打印电子邮件ID和用户名。

请帮助。我是这一切的新手。

以下是我正在使用的代码 -

<?php 
session_start();
require_once "C:/xampp/htdocs/uttam/src/facebook.php"; 

$config = array(
    "appId" => '<removed>',
    "secret" => '<removed>');

$fb = new Facebook($config);

$user = $fb->getUser();

?>  

<html> 
 <head> 
  <title>Hello Facebook</title> 
 </head> 
 <body>
<?php
if (!$user) { 
    $params = array(
        "scope" => "read_stream,publish_stream,user_photos",
        "redirect_uri" => 'http://localhost/uttam');
    echo '<a href="' . $fb->getLoginUrl($params) . '">Login</a>'; 
}
else { 
?>
  <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post" enctype="multipart/form-data">
   <textarea name="message" id="message" rows="2" cols="40"></textarea><br>
   <input type="file" name="image" id="image"><br>
   <input type="submit" value="Update"> 
  </form> 
<?php
    // process form submission
    if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["message"])) {
        if (is_uploaded_file($_FILES["image"]["tmp_name"])) { 
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($finfo, $_FILES["image"]["tmp_name"]);
            $allowed = array("image/gif", "image/jpg", "image/jpeg", "image/png");
            // upload image
            if (in_array($mime, $allowed)) { 
                $data = array(
                    "name" => $_POST["message"],
                    "image" => "@" . realpath($_FILES["image"]["tmp_name"]));
                $fb->setFileUploadSupport(true); 
                $status = $fb->api("/me/photos", "POST", $data);    
            }
        }
        else {
            // update status message
            $data = array("message" => $_POST["message"]);
            $status = $fb->api("/me/feed", "POST", $data); 
        }
    } 
    if (isset($status)) {
        echo "<pre>" . print_r($status, true) . "</pre>";
    } 
}
?>
 </body> 
</html>

2 个答案:

答案 0 :(得分:1)

您最好在html中尝试javascript代码。试试这些facebook link

对我而言,它运作良好

在代码中: function testAPI() { console.log('Welcome! Fetching your information.... '); FB.api('/me', function(response) { console.log("******get full response******"+JSON.stringify(response)+"********"); document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!'; }); }

答案 1 :(得分:0)

您可以使用更新的Facebook SDK for PHP v4

您可以通过Google搜索找到教程。其中之一就是 tutorial