編輯問題的用戶:PHP Facebook的SDK不創建在WordPress
林試圖讓我使用WordPress網站Facebook登錄插件this sdk
當我在Facebook上點擊鏈接登錄或註冊它打開一個小窗口通過Facebook說,繼續,但比它不創建wordpress數據庫中的用戶和打破wordpress頁腳,所以它顯示An active access token must be used to query information about the current user.
消息頁面刷新後的登錄表單。
我完全Facebook登錄的PHP代碼是這一個:
<?php
include 'facebook/facebook.php';
function gazi_fb_loginForm(){
gazi_fb_error_message();
gazi_fb_LoadScript();
if(is_user_logged_in()==false){
?>
<div class="facebook_wrapper">
<img src="<?php echo plugin_dir_url(__FILE__).'images/facebook_or.png';?>" style="border:none; box-shadow:none;">
<br>
<a href="javascript:void(0)" onClick="FBLogin();">
<img src="<?php echo plugin_dir_url(__FILE__).'images/facebook_button.png';?>" alt="Fb Connect" title="Login with facebook" /></a></div>
<?php
}
}
function gazi_fb_LoadScript(){
global $wpdb;
$gazi_option=$wpdb->prefix."gazi_option";
$path = plugin_dir_url(__FILE__); // define path to link and scripts
$pageURL = get_permalink();
$sign = strpos($pageURL,'?')?'&':'?';
//facebook app secret
$qry1="SELECT value FROM $gazi_option WHERE fieldname='gazi_facebook_app_secret'";
$facebook_app_secret = $wpdb->get_var($qry1);
$qry2="SELECT value FROM $gazi_option WHERE fieldname='gazi_facebook_app_id'";
$facebook_app_id = $wpdb->get_var($qry2);
$facebook = new Facebook(array(
'appId' => $facebook_app_id,
'secret' => $facebook_app_secret,
));
?>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook_app_id; ?>',
xfbml : true,
status : true,
cookie : true,
version : 'v2.1'
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
}
else {
console.log('initiate FB login...');
FB.login();
}
});
FB.api('/me/feed',function(response){
var idDiv=document.getElementById('result');
idDiv.textContent=JSON.stringify(response);
});
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function FBLogin(){
FB.login(function(response){
if(response.authResponse){
window.location.href = "<?php echo $pageURL.$sign;?>option=fblogin";
}
}, {scope: 'email'});
}
</script>
<?php
}
function gazi_fb_error_message(){
if(isset($_SESSION['msg'])){
echo '<div class="'.$_SESSION['msg_class'].'">'.$_SESSION['msg'].'</div>';
unset($_SESSION['msg']);
unset($_SESSION['msg_class']);
}
}
function gazi_fb_login_validate(){
$path = plugin_dir_url(__FILE__); // define path to link and scripts
$pageURL = get_permalink();
$sign = strpos($pageURL,'?')?'&':'?';
if(isset($_REQUEST['option']) && $_REQUEST['option'] == "fblogin"){
global $wpdb;
$gazi_option=$wpdb->prefix."gazi_option";
//facebook app secret
$qry1="SELECT value FROM $gazi_option WHERE fieldname='gazi_facebook_app_secret'";
$facebook_app_secret = $wpdb->get_var($qry1);
$qry2="SELECT value FROM $gazi_option WHERE fieldname='gazi_facebook_app_id'";
$facebook_app_id = $wpdb->get_var($qry2);
$facebook = new Facebook(array(
'appId' => $facebook_app_id,
'secret' => $facebook_app_secret,
'cookie' => TRUE,
));
$fbuser = $facebook->getUser();
if ($fbuser) {
try {
$user_profile = $facebook->api('/me');
}
catch (Exception $e) {
echo $e->getMessage();
exit();
}
if (!isset($user_profile['email'])) $user_profile['email'] = $user_profile['id'] . '@facebook.com';
$user_fbid = $fbuser;
$user_email = $user_profile["email"];
$user_fnmae = $user_profile["first_name"];
if(email_exists($user_email)) { // user is a member
$user = get_user_by('login', $user_email);
$user_id = $user->ID;
wp_set_auth_cookie($user_id, true);
} else { // this user is a guest
$random_password = wp_generate_password(10, false);
$user_id = wp_create_user($user_email, $random_password, $user_email);
update_user_meta($user_id, 'avtar_image', 'https://graph.facebook.com/' . $user_profile['id'] . '/picture?type=large');
wp_update_user(array(
'ID' => $user_id,
'display_name' => $user_profile['name'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name']
));
wp_set_auth_cookie($user_id, true);
}
wp_redirect($pageURL.$sign.'login4=1');
exit;
}
}
}
?>
這裏是在上面的代碼中的JavaScript。
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook_app_id; ?>',
xfbml : true,
status : true,
cookie : true,
version : 'v2.1'
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
}
else {
console.log('initiate FB login...');
FB.login();
}
});
FB.api('/me/feed',function(response){
var idDiv=document.getElementById('result');
idDiv.textContent=JSON.stringify(response);
});
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function FBLogin(){
FB.login(function(response){
if(response.authResponse){
window.location.href = "<?php echo $pageURL.$sign;?>option=fblogin";
}
}, {scope: 'email'});
}
</script>
有沒有人知道如何使它工作?我做什麼錯了?任何幫助,非常感謝。
三江源。
UPDATE:
我已經改變使用這個答案在我的文件中的一些行here
改變了這個: $ fbuser = $ facebook->的getUser();
if ($fbuser) {
try {
$user_profile = $facebook->api('/me');
}
catch (Exception $e) {
echo $e->getMessage();
exit();
}
這一個:
$fbuser = $facebook->getUser();
$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
if ($fbuser) {
try {
// We have a valid FB session, so we can use 'me'
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
} catch (FacebookApiException $e) {
error_log($e);
}
我現在得到了註冊,但它很奇怪
的WordPress的用戶名和暱稱爲八方通@facebook:d
所以我覺得它沒有得到這裏的變量:
if (!isset($user_profile['email'])) $user_profile['email'] = $user_profile['id'] . '@facebook.com'; and just keeping @facebook instead without the `$user_profile['id']`
嘗試的onclick代替的onClick。 – Ozan
感謝您的回答,eyvallah,但沒有運氣它是相同的,當我點擊按鈕什麼都沒有再次發生和控制檯中的相同錯誤。 – Gazi
我現在不可能運行代碼,但它也可能超出範圍。 – Ozan