dongyan9950 2015-08-21 23:58
浏览 46
已采纳

PHP:发布关于提交表单的推文

I've a template which contains a form for the user to fill. After the user successfully logs in using his/her Twitter account, the user is redirected to the template containing the form. Now, I'm trying to implement a feature wherein a tweet is posted on the user's Twitter timeline after the user submits the form.

I'm using Abraham's twitteroauth to implement Twitter OAuth, and also to post tweets on behalf of the user.

I'm also using the save_post hook to trigger a function which will post the tweet on user's Twitter account as soon as the form is submitted.

Code:

<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>

As seen above, I've defined the following hook to trigger the function cron_tweet as soon as the Submit button is clicked by the user:

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);

The cron_tweet function basically attempts to post the tweet on user's Twitter account.

However, the above code doesn't seem to work as intended. After the user submits the form, no tweet is being posted on the user's Twitter account.

What seems to be wrong with my code? Am I missing something?

  • 写回答

1条回答 默认 最新

  • dscqrkvr9562034621 2015-08-22 05:24
    关注

    Are you seeing any errors? The most likely problem I can see are that $o_token and $o_token_secret can not be seen from in the scope of cron_tweet.

    One solution would be to save those details into a session variable:

            $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'));
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题