WordPress 和 Facebook php sdk 4.0
Wordpress and Facebook php sdk 4.0
我的 wordpress 4.1 安装了以下插件:
- iThemes 安全
- WordPress 搜索引擎优化
我在facebook sdk中插入了一个函数。
这是代码:
//Footer
add_action('wp_footer', 'my_footer');
function my_footer() {
$post_to_check = get_post(get_the_ID());
if ( is_singular() && has_shortcode( $post_to_check->post_content, 'myshortcode' ) ) {
?>
<script>
var DEBUG = true;
jQuery(document).ready(function($){
$.ajax({
type : "POST",
url : "index.php",
data : { action : "value" },
success : function(response){
// the server has finished executing PHP and has returned something, so display it!
if(DEBUG){ console.log('AJAX done...'); }
}
});
});
</script>
<?php
}
}
//ajax
add_action('init', 'my_request');
function my_request() {
if ( isset($_POST['action']) && $_POST['action'] == 'value' ) {
session_start();
require_once __DIR__ . "/sdk/autoload.php";
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSDKException;
$app_id = getAppId();
$app_secret = getAppSecret();
$rdr_fb_url = curPageURL();
$required_scope = 'public_profile, email';
if(empty($app_id) || empty($app_secret))
exit('Error.');
FacebookSession::setDefaultApplication($app_id , $app_secret);
$helper = new FacebookRedirectLoginHelper($rdr_fb_url);
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
// When validation fails or other local issues
die(" Error : " . $ex->getMessage());
}
var_dump($session);
if ($session){
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
echo 'Hi'.$user_profile->getName();
}else{
//display login url
$login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
}
?>
<div class="login">
<a href="<?php echo $login_url; ?>">Login</a>
</div>
<?php
exit();
}
}
如果我在 wordpress 之外使用它,效果很好。
在 wp var_dump($session);检索始终为空。 (没有给出错误)即使在接受申请条款之后。 url.
中的密码好像是拿不到一样
我也试过禁用插件,但没有。
我需要php,请不要告诉我使用javascript。
抱歉我的英语不好:P
编辑:
我使用永久链接:
http://www.myste.com/%year%/%monthnum%/%postname%/
我认为问题是隐藏 facebook get 的重写规则。
已解决:我在 ajax 调用之外输入代码,在 function my_footer() {
我的 wordpress 4.1 安装了以下插件:
- iThemes 安全
- WordPress 搜索引擎优化
我在facebook sdk中插入了一个函数。
这是代码:
//Footer
add_action('wp_footer', 'my_footer');
function my_footer() {
$post_to_check = get_post(get_the_ID());
if ( is_singular() && has_shortcode( $post_to_check->post_content, 'myshortcode' ) ) {
?>
<script>
var DEBUG = true;
jQuery(document).ready(function($){
$.ajax({
type : "POST",
url : "index.php",
data : { action : "value" },
success : function(response){
// the server has finished executing PHP and has returned something, so display it!
if(DEBUG){ console.log('AJAX done...'); }
}
});
});
</script>
<?php
}
}
//ajax
add_action('init', 'my_request');
function my_request() {
if ( isset($_POST['action']) && $_POST['action'] == 'value' ) {
session_start();
require_once __DIR__ . "/sdk/autoload.php";
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSDKException;
$app_id = getAppId();
$app_secret = getAppSecret();
$rdr_fb_url = curPageURL();
$required_scope = 'public_profile, email';
if(empty($app_id) || empty($app_secret))
exit('Error.');
FacebookSession::setDefaultApplication($app_id , $app_secret);
$helper = new FacebookRedirectLoginHelper($rdr_fb_url);
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
// When validation fails or other local issues
die(" Error : " . $ex->getMessage());
}
var_dump($session);
if ($session){
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
echo 'Hi'.$user_profile->getName();
}else{
//display login url
$login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
}
?>
<div class="login">
<a href="<?php echo $login_url; ?>">Login</a>
</div>
<?php
exit();
}
}
如果我在 wordpress 之外使用它,效果很好。 在 wp var_dump($session);检索始终为空。 (没有给出错误)即使在接受申请条款之后。 url.
中的密码好像是拿不到一样我也试过禁用插件,但没有。
我需要php,请不要告诉我使用javascript。
抱歉我的英语不好:P
编辑: 我使用永久链接: http://www.myste.com/%year%/%monthnum%/%postname%/
我认为问题是隐藏 facebook get 的重写规则。
已解决:我在 ajax 调用之外输入代码,在 function my_footer() {