我使用Facebook API,並希望檢索連接用戶的Facebook信息。我可以在javascript中檢索信息,但我想要獲取信息以填充存儲在我的數據庫中的PHP。如何使用Facebook PHP API獲取用戶信息
這是我在javascript函數代碼:
<html>
<head>
<title>My Application</title>
<style type="text/css">
div { padding: 10px; }
</style>
<meta charset="UTF-8">
</head>
<body>
<div id="fb-root"></div>
<script type="text/javascript">
var fbAppId = 'myAppId';
var objectToLike = 'http://techcrunch.com/2013/02/06/facebook-launches-developers-live-video-channel-to-keep-its-developer-ecosystem-up-to-date/';
if (fbAppId === 'replace me') {
alert('Please set the fbAppId in the sample.');
}
window.fbAsyncInit = function() {
FB.init({
appId : fbAppId, // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse page for xfbml or html5 social plugins like login button below
});
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
window.alert(response.last_name + ', ' + response.first_name + ", " + response.email);
});
}
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>
這是我的PHP代碼,我不能讓它工作
<?php
require_once("php-sdk/facebook.php");
$config = array();
$config['appId'] = 'myAppId';
$config['secret'] = 'myCodeSecret';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$user = $facebook->getUser();
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
?>
如果我顯示變量$user
,我讓我的用戶ID。但是我無法獲得其他信息。
我看起來更詳細,這可能是在Facebook的應用程序的配置問題。你能否解釋在Facebook上創建應用程序的步驟?
嘗試的var_dump($ user_profile);讓我們看看什麼是響應 – Fabio
這會返回NULL NULL – Florent
這意味着您的調用失敗,代碼或參數可能有問題,您是否正確設置了登錄信息? – Fabio