Forum Hello people, my name is Edwin Galician and I have a problem about the Facebook API in its version 5 and consists of the following, it turns out that on my local server, I work well, but the production version generates me inconvientens the most important so far is not out of my user profile (logout), if someone could explain me or help them to be grateful.
<?php
session_start();
//script echo por edwin gallego , este codigo se inserta antes del callback para que funcione la session.
foreach ($_COOKIE as $k=>$v) {
if(strpos($k, "FBRLH_")!==FALSE) {
$_SESSION[$k]=$v;
}
}
//fin del script
if (isset($_REQUEST['news'])) {
$_SESSION['news'] = $_REQUEST['news'];
}
if (isset($_REQUEST['redirect_to'])) {
$_SESSION['redirect_to'] = $_REQUEST['redirect_to'];
}
get_template_part('inc/php/lib/facebook-sdk-v5/autoload');
$register_page_id = get_theme_option('register_page');
$fb = new Facebook\Facebook([
'app_id' => get_theme_option('facebook_app_id'),
'app_secret' => get_theme_option('facebook_app_secret'),
'default_graph_version' => 'v2.5'
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// getting short-lived access token
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// getting basic info about user
try {
$profile_request = $fb->get('/me?fields=id,name,first_name,middle_name,last_name,email,gender,link,timezone,hometown,verified,name_format');
//get image
$requestPicture = $fb->get('/me/picture?redirect=false&height=50'); //getting user picture
$picture = $requestPicture->getGraphUser();
$profile = $profile_request->getGraphNode()->asArray();
//get devices
$profile_request_device = $fb->get("/me?fields=devices");
$profile_devices = $profile_request_device->getGraphNode()->asArray();
$profile_device_os= $profile_devices['devices'];
$device_os_name= $profile_device_os[0];
$os_name=$device_os_name['os'];
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
//logout
$logoutUrl = $helper->getLogoutUrl($accessToken, get_bloginfo('url'));
$id=$profile['id'];
$first_name=(isset($profile['first_name'])) ? $profile['first_name'] : '';
$middle_name=(isset($profile['middle_name'])) ? $profile['middle_name'] : '';
$last_name=(isset($profile['last_name'])) ? $profile['last_name'] : '';
$email=$profile['email'];
$gender=(isset($profile['gender'])) ? $profile['gender'] : '';
$link=(isset($profile['link'])) ? $profile['link'] : '';
$verified=(isset($profile['verified'])) ? $profile['verified'] : '';
$pic_profile='http://graph.facebook.com/' . $id . '/picture';
$news = (isset($_SESSION['news'])) ? $_SESSION['news'] : '';
$password = base64_encode($id);
if (username_exists($id)) {
$credentials = array(
'user_login' => $id,
'user_password' => $password,
'remember' => true
);
$user = wp_signon($credentials, true);
if (!is_wp_error($user)) {
if (isset($_SESSION['redirect_to']) && $_SESSION['redirect_to'] != '') {
$redirect_to = $_SESSION['redirect_to'];
} else if (isset($_REQUEST['redirect_to']) && $_REQUEST['redirect_to'] != '') {
$redirect_to = $_REQUEST['redirect_to'];
} else {
$redirect_to = get_home_uri();
}
wp_redirect($redirect_to);
exit;
}
} else if (email_exists( $email )) {
$user = get_user_by('email', $email);
if (!is_facebook_user($user->ID)) {
wp_redirect(get_permalink(get_theme_option('login_page')) . '/plan-ciudad?from=facebook');
exit;
} else {
wp_redirect(get_permalink(get_theme_option('login_page')) . '/plan-ciudad');
exit;
}
} else {
$temporal_username = $id;
$temporal_id = username_exists($temporal_username);
$temporal_email = email_exists($email);
if (!$temporal_id && !$temporal_email) {
$user_id = wp_create_user($temporal_username, $password, $email);
$the_user = wp_update_user(array(
'ID' => $user_id,
'user_nicename' => sanitize_title($temporal_username),
'display_name' => $first_name . ' ' . $last_name,
'first_name' => $first_name,
'last_name' => $last_name
));
if (!is_wp_error($the_user)) {
$active_key = sha1($email . 'usr' . $user_id . strtotime(date('U')));
$the_user_meta = array(
'fb_id' => $id,
'fb_link' => $link,
'gender' => $gender,
'os' => $os_name,
'active_key' => $active_key,
'active' => 'yes'
);
if ($news) {
$the_user_meta['newsletter'] = $news;
}
foreach ($the_user_meta as $key => $value) {
add_user_meta($user_id, $key, $value);
}
update_field('facebook_avatar', $pic_profile, 'user_' . $user_id);
update_field('facebook', $link, 'user_' . $user_id);
$credentials = array(
'user_login' => $id,
'user_password' => $password,
'remember' => true
);
$user = wp_signon($credentials, true);
wp_redirect(get_permalink($register_page_id) . '/finalizar');
exit;
}
}
}
} else {
$redirectURL = get_permalink($register_page_id) . '/facebook';
// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here
$loginUrl = $helper->getLoginUrl($redirectURL, $permissions);
//este codigo se usa para quitar el error en facebook sdk crossline y es
//echo por edwin gallego email:emgallego(arroba)elpais.com.co
foreach ($_SESSION as $k=>$v) {
if(strpos($k, "FBRLH_")!==FALSE) {
if(!setcookie($k, $v)) {
//what??
} else {
$_COOKIE[$k]=$v;
}
}
}
//fin del script echo por edwin gallego
wp_redirect($loginUrl);
exit;
}
//$loginUrl = $helper->getLoginUrl('fb-callback.php', $permissions);
?>