J7mbo / twitter-api-php

The simplest PHP Wrapper for Twitter API v1.1 calls
MIT License
1.82k stars 802 forks source link

Plugin to auto post to twitter for (Question2Answer) #151

Closed dantelex closed 8 years ago

dantelex commented 9 years ago

Please trying to work using TwitterAPIExchange. help me out

This code doesn’t work. It’s supposed to get the post and it’s url and send to twitter. it doesn’t show any error. the problem is from the “build_twitter_status” cause that’s what’s going to be pages to $postfields.

       //This registers the events and gets the post and sends it to twitter
        public function process_event($event, $userid, $handle, $cookieid, $params) {
           require_once QA_INCLUDE_DIR . 'qa-app-emails.php';
           require_once QA_INCLUDE_DIR . 'qa-app-format.php';
           require_once QA_INCLUDE_DIR . 'qa-util-string.php';

        switch ($event) {
            //this gets question posts
            case 'q_post':
                $sendhandle=isset($handle) ? $handle : qa_lang('main/anonymous');
                    $this->post_tweets(
                    $this->build_twitter_status(
                            $sendhandle,
                            $params['title'],
                            $params['text'],
                                qa_q_path($params['postid'], $params['title'], true) //url of the questions post
                        )   
                    );
            break;

            //this get answer posts
            case 'a_post':
                $sendhandle=isset($handle) ? $handle : qa_lang('main/anonymous');
                    $this->post_tweets(
                    $this->build_twitter_status(
                            $sendhandle,
                            $params['title'],
                            $params['text'],
                                qa_q_path($params['postid'], $params['title'], true, 'A') //url of the questions post
                        )   
                    );
            break;

            //this gets comment posts
            case 'c_post':

                $sendhandle=isset($handle) ? $handle : qa_lang('main/anonymous');
                    $this->post_tweets(
                        $this->build_twitter_status(
                            $sendhandle,
                            $params['title'],
                            $params['text'],
                                qa_q_path($params['postid'], $params['title'], true, 'C')//url of the questions post
                        )   
                    );
        }
    }

    //This is the status message that will go to twitter
    private function build_twitter_status($who, $title, $content, $url) {
        return array(
            'status' => array(
                'message[body]' => sprintf("%s asked a new question \"%s\" on #QNA, check it out!", $who, $title),
                'message[url]' => $url,
            )
        );
    }

    //This is the twitter request
    function post_tweets()
    {

        require_once $this->directory . 'TwitterAPIExchange.php';
        // Setting our Authentication Variables that we got after creating an application
        $settings = array(
            'oauth_access_token' => qa_opt('qa_twitter_at'),
            'oauth_access_token_secret' => qa_opt('qa_twitter_ts'),
            'consumer_key' => qa_opt('qa_twitter_ck'),
            'consumer_secret' => qa_opt('qa_twitter_cs')
        );

        $url = "https://api.twitter.com/1.1/statuses/update.json";
        $requestMethod = "POST";

        //This is where ths post status goes
        $postfields = $this->build_twitter_status();

        $twitter = new TwitterAPIExchange($settings);
        $tweets = $twitter->setPostfields($postfields)
            ->buildOauth($url, $requestMethod)
                ->performRequest();
        return $tweets;
    }

While this code works, but it only makes the posts send to twitter but not the url of the post too. i want to be able to send the post and it’s url. the problem is from the “build_twitter_status” cause that’s what’s going to be pages to $postfields

   public function process_event($event, $userid, $handle, $cookieid, $params) {
        require_once QA_INCLUDE_DIR . 'qa-app-emails.php';
        require_once QA_INCLUDE_DIR . 'qa-app-format.php';
        require_once QA_INCLUDE_DIR . 'qa-util-string.php';

        switch ($event) {
            case 'q_post':
                $sendhandle=isset($handle) ? $handle : qa_lang('main/anonymous');
                    $this->post_tweets(
                    $sendhandle,
                    $params['title'],
                    $params['text'],
                        qa_q_path($params['postid'], $params['title'], true)
                    );
            break;

            case 'a_post':
                $sendhandle=isset($handle) ? $handle : qa_lang('main/anonymous');
                    $this->post_tweets(
                    $sendhandle,
                    $params['title'],
                    $params['text'],
                        qa_q_path($params['postid'], $params['title'], true, 'A')
                    );
            break;

            case 'c_post':

                $sendhandle=isset($handle) ? $handle : qa_lang('main/anonymous');
                    $this->post_tweets(
                    $sendhandle,
                    $params['title'],
                    $params['text'],
                        qa_q_path($params['postid'], $params['title'], true, 'C')   
                    );
        }
    }

    private function build_twitter_status() {
        $tweetquestion = $_POST[’title’]; // get’s the question posts
        $tweetanswer = $_POST['a_content’]; // get’s the answer posts
        $tweetcomment = $_POST[‘c_content’]; // get’s the comment posts

        if($tweetquestion) {
            return $tweetquestion;
        }
        else if($tweetanswer) {
            return $tweetanswer;
        }
        else if($tweetcomment) {
            return $tweetcomment;
        }
    }

    function post_tweets()
    {

        require_once $this->directory . 'TwitterAPIExchange.php';
        // Setting our Authentication Variables that we got after creating an application
        $settings = array(
            'oauth_access_token' => qa_opt('qa_twitter_at'),
            'oauth_access_token_secret' => qa_opt('qa_twitter_ts'),
            'consumer_key' => qa_opt('qa_twitter_ck'),
            'consumer_secret' => qa_opt('qa_twitter_cs')
        );

        $url = "https://api.twitter.com/1.1/statuses/update.json";
        $requestMethod = "POST";

        $postfields = array(
            'status' => $this->build_twitter_status()
        );
        $twitter = new TwitterAPIExchange($settings);
        $tweets = $twitter->setPostfields($postfields)
            ->buildOauth($url, $requestMethod)
                ->performRequest();
        return $tweets;
    }
J7mbo commented 8 years ago

Hi, this is an old one, closing for now but if you do need help please feel free to reopen a new issue!