因此,我正在开发一个应用程序,您可以在其中发送一首爱情诗,然后将其恢复到您的Facebook墙上翻译。
所以这应该是一个非常简单的任务。我要求access_token和user_id:他们填写表格然后发给我。
当我将应用程序移动到页面选项卡时,我首次遇到重定向问题。
现在发生的事情是,当他在我们的粉丝页面时,我们不再获得“客户”的用户名,尽管他正在访问该应用程序....
因此,一名创始人。这可能取决于:这是我的代码:
<?php
/*
Template Name: Febuary Love app
*/
?>
<?php
require ('fb-php-sdk/facebook.php');
//Create facebook application instance.
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxxxxxx',
'cookie' => true,
));
//get user- if present, insert/update access_token for this user
$user = $facebook->getUser();
if($user){
try {
$userData = $facebook->api('/me');
} catch (FacebookApiException $e) {
die("API call failed");
}
$access_token = $facebook->getAccessToken();
}
else {
echo "Please login...<br />";
$my_redir_url = 'http://www.facebook.com/pages/Copypanthers/131063547012851?sk=app_286456534743923';
$dialog_url = "http://www.facebook.com/dialog/oauth?scope=user_about_me,publish_stream,offline_access&client_id=286456534743923&redirect_uri=" . urlencode($my_redir_url);
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
echo $user;
//create authorising url
if(!$user){
$loginUrl = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'offline_access,publish_stream',
'redirect_uri' => 'http://apps.facebook.com/february_love'
));
}
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter the Poem name';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter some notes';
}
$tags = $_POST['post_tags'];
$access_token = $_POST['access_token'];
$user = $_POST['user'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => array($_POST['cat']), // Usable for custom taxonomies too
'tags_input' => array($tags),
'post_status' => 'draft', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post', //'post',page' or use a custom post type if you want to
'access_token' => $access_token,
'user' => $user,
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
add_post_meta($pid, 'access_token', $access_token, true);
add_post_meta($pid, 'user', $user, true);
//SET OUR TAGS UP PROPERLY
wp_set_post_tags($pid, $_POST['post_tags']);
//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect();
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');
get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="form-content">
<!-- WINE RATING FORM -->
<div class="wpcf8">
<a href="<?php echo $loginUrl; ?>" target="_top">
<div id="steps-app">
<div id="step1">
<img src="https://www.copypanthers.com/wp-content/uploads/2012/02/step1.png">
<p>First <em>allow</em> the app so we can send you the translated poem</p>
</div>
<div id="app_center">
<img src="https://www.copypanthers.com/wp-content/uploads/2012/02/accept_app.png">
</div>
<div id="step2">
<img src="https://www.copypanthers.com/wp-content/uploads/2012/02/step2.png">
<p>Then send us the poem or message</p>
</div>
<div id="arrow">
<img src="https://www.copypanthers.com/wp-content/uploads/2012/02/narrow.png">
</div>
</div></a>
<div class="content-app">
<?php the_content(); ?>
<h2>Share some February Love!</h2>
<p>Although the 14th of February should be dedicated to your loved one, we think that friends, bosses or favorite co-workers deserve some affection, too. <br/><br/>So until the 13th of February we will translate a poem or message from you to them <em>for free</em>. We will also reward 2 writers with a special <em>Valentine’s Day gift.</em><br/><br/> </p>
<form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
<!-- post name -->
<fieldset name="name">
<label for="title">Title of your poem or message:</label>
<input type="text" id="title" value="" tabindex="5" name="title" />
</fieldset>
<!--<fieldset class="dedication">
<label for="dedication">Dedicated to (all my co-workers for example):</label>
<input type="text" value="" tabindex="10" id="dedication" name="dedication" />
</feildset> -->
<!-- post Category -->
<fieldset class="category">
<label for="cat">Translate to:</label>
<?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?>
</fieldset>
<!-- post Content -->
<fieldset class="content">
<label for="description">Love poem:</label>
<textarea id="description" tabindex="15" name="description" cols="70" rows="5"></textarea>
</fieldset>
<!-- post tags -->
<fieldset class="tags">
<label for="post_tags"></label>
<input type="hidden" value="" tabindex="35" name="post_tags" id="post_tags" />
</fieldset>
<fieldset class="submit">
<input type="submit" value="Send" tabindex="40" id="submit" name="submit" />
</fieldset>
<fieldset class="access_token">
<label for="access_token"></label>
<input type="text" value="<?php echo $access_token;?>" id="access_token" name="access_token" />
</fieldset>
<fieldset class="user">
<label for="user"></label>
<input type="text" value="<?php echo $user;?>" id="user" name="user" />
</fieldset>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div><!-- END content-app -->
</div> <!-- END WPCF7 -->
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>
坦克需要时间。
祝你好运
答案 0 :(得分:0)
当您的应用程序位于页面标签中时,您将收到signed_request
。解码后,signed_request
将包含[部分]用户信息(有关详细信息,请参阅上面的链接)。解码signed_request
后,您将能够提取用户ID。从那里开始,需要查询Graph API以检索应包含其名称的用户(公共)信息。查询看起来像这样:
$facebook->api('/USER_ID');
即使对于未对您的应用程序进行身份验证的用户,您也应该能够检索此最小数据:
{
"id": "123",
"name": "'John Skeet",
"first_name": "'John",
"last_name": "Skeet",
"link": "https://www.facebook.com/profile.php?id=123",
"gender": "male",
"locale": "en_UK",
"updated_time": "2012-01-30T01:09:29+0000",
"type": "user"
}