편집 해당 사용자를 만들지 않습니다 :PHP는 페이스 북 SDK는 워드 프레스
임 사용하여 내 워드 프레스 사이트에 페이스 북 로그인 플러그인을 만들려고 노력 this sdk
나는 로그인 페이스 북 버튼을 클릭하면 또는 그것이 계속된다고 말하는 페이스 북을 통해 작은 창을 열지 만, wordpress 데이터베이스에서 사용자를 생성하고 wordpress footer를 깨지 않는 것보다 페이지가 새로 고침 후 로그인 양식 아래에 An active access token must be used to query information about the current user.
messagge를 표시합니다.
내 전체 페이스 북 로그인 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;
}
}
}
?>
위의 코드에서 자바 스크립트입니다.
<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>
어떻게 작동시키는 지 알고 계십니까? 어떤 잘못된 일을하고 있습니까? 어떤 도움을 주셔서 감사합니다.
고마워요.
이이 업데이트 :
내가 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);
}
지금 등록되었다하지만 워드 프레스의 매우 이상한
사용자 이름과 별명 이름에 allways @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을 대신 온 클릭의 시도. – Ozan
답변을 주셔서 감사합니다, eyvallah,하지만 행운을 빕니다. 버튼을 클릭해도 아무 것도 다시 일어나지 않으며 콘솔에서도 같은 오류가 발생합니다. – Gazi
지금 당장은 코드를 실행할 가능성이 없지만 범위를 벗어날 수 있습니다. – Ozan