我有一个模板,其中包含供用户填写的表单。用户使用他/她的Twitter帐户成功登录后,用户将被重定向到包含该表单的模板。现在,我正在尝试实施一项功能,即在用户提交表单后,在用户的Twitter时间轴上发布推文。
我使用亚伯拉罕twitteroauth
来实施Twitter OAuth,并代表用户发布推文。
我还使用save_post
挂钩来触发一个功能,该功能会在提交表单后立即在用户的Twitter帐户上发布推文。
代码:
<pre>
<?php
/*
*Template Name: Callback
*/
?>
<?php
session_start();
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', "XXXXXXXXXXX");
define('CONSUMER_SECRET', "XXXXXXXXXXX");
define('OAUTH_CALLBACK', "http://localhost/wordpress/index.php/callback/");
$request_token = [];
$request_token['oauth_token'] = $_SESSION['oauth_token'];
$request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];
if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token'])
{
echo "Opps! Something went wrong!";
}
else
{
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
//print_r($access_token);
$_SESSION['access_token'] = $access_token;
$access_token = $_SESSION['access_token'];
//print_r($_REQUEST['oauth_verifier']);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$handle = $access_token['screen_name'];
$o_token = $access_token['oauth_token'];
$o_token_secret = $access_token['oauth_token_secret'];
$user_id = $access_token['user_id'];
$o_verifier = $_REQUEST['oauth_verifier'];
function cron_tweet()
{
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $o_token, $o_token_secret);
$response = $connection->post("statuses/update", array('status' => 'Versionuyyu 1.0'));
}
add_action('save_post', cron_tweet);
}
?>
<script>
var count = 0
function addNewMessage(count)
{
if(count > 5)
{
window.alert("NO MORE THAN 5!");
}
else
{
var celeb = document.createElement("input");
celeb.type = "text";
celeb.name = "tweet" + count;
celeb.placeholder = "Tweet" + " " + count;
celebrity.appendChild(celeb);
var date = document.createElement("input");
date.type = "datetime-local";
date.name = "date" + count;
date.placeholder = "message-date" + " " + count;
celebrity.appendChild(date);
celebrity.appendChild(document.createElement("br"));
celebrity.appendChild(document.createElement("br"));
}
}
function postResults()
{
<?php
$post_information = array(
'post_title' => 'New Tweet Schedule',
'post_content' => 'Mandatory content',
'post_status' => 'publish',
'post_type' => 'schedule_tweet',
);
$post_id = wp_insert_post( $post_information );
add_post_meta($post_id, 'twitter_handle', $handle, true);
add_post_meta($post_id, 'oauth_token', $o_token, true);
add_post_meta($post_id, 'oauth_token_secret', $o_token_secret, true);
add_post_meta($post_id, 'user_id', $user_id, true);
add_post_meta($post_id, 'tweet_1', $_POST['tweet1'], true);
add_post_meta($post_id, 'tweet_2', $_POST['tweet2'], true);
add_post_meta($post_id, 'tweet_3', $_POST['tweet3'], true);
add_post_meta($post_id, 'tweet_4', $_POST['tweet4'], true);
add_post_meta($post_id, 'tweet_5', $_POST['tweet5'], true);
add_post_meta($post_id, 'date_1', $_POST['date1'], true);
add_post_meta($post_id, 'date_2', $_POST['date2'], true);
add_post_meta($post_id, 'date_3', $_POST['date3'], true);
add_post_meta($post_id, 'date_4', $_POST['date4'], true);
add_post_meta($post_id, 'date_5', $_POST['date5'], true);
?>
}
</script>
<form method = "POST">
<fieldset>
<a style = "color:red" onclick = "addNewMessage(++count)">Schedule a tweet</a>
<div id = "celebrity"/>
</fieldset>
<br>
<fieldset>
<input type="hidden" name="submitted" id="submitted" value="true" />
<?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
<button type="submit" onclick = "postResults()"><?php _e('Add Campaign', 'framework') ?></button>
</fieldset>
</form>
如上所示,我已经定义了以下挂钩,以便在用户点击cron_tweet
按钮后立即触发功能Submit
:
function cron_tweet()
{
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $o_token, $o_token_secret);
$response = $connection->post("statuses/update", array('status' => 'Versionuyyu 1.0'));
}
add_action('save_post', cron_tweet);
cron_tweet
函数基本上试图在用户的Twitter帐户上发布推文。
但是,上述代码似乎没有按预期工作。用户提交表单后,用户的Twitter帐户上不会发布推文。
我的代码似乎有什么问题?我错过了什么吗?
答案 0 :(得分:1)
您是否看到任何错误?我可以看到的最可能的问题是,在$o_token
的范围内无法看到$o_token_secret
和cron_tweet
。
一种解决方案是将这些细节保存到会话变量中:
$handle = $access_token['screen_name'];
$_SESSION['o_token'] = $access_token['oauth_token'];
$_SESSION['o_token_secret'] = $access_token['oauth_token_secret'];
$user_id = $access_token['user_id'];
$o_verifier = $_REQUEST['oauth_verifier'];
function cron_tweet()
{
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['o_token'], $_SESSION['o_token_secret']);
$response = $connection->post("statuses/update", array('status' => 'Versionuyyu 1.0'));
}