dongyan9950 2015-08-21 15: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-21 21: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'));
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部