Twitteroauth-master


#1

Hi,
I am using “PHP library for working with Twitter’s OAuth API.” But the code is available for only redirecting to twitter and then posting to twitter wall. I want to post the status message without redirecting to twitter site.

Below is the sample code to post .

<?php
// Build TwitterOAuth object with client credentials.
        $this->connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

        // Get temporary credentials.
        $request_token = $this->connection->getRequestToken(OAUTH_CALLBACK);

        // Save temporary credentials to session.
        $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
        $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

        $this->connection->http_code.'http code';
        /* If last connection failed don't display authorization link. */
        switch ($this->connection->http_code) {
            case 200:
                /* Build authorize URL and redirect user to Twitter. */
               echo $url = $this->connection->getAuthorizeURL($token);
               
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
                curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");                
                $page = curl_exec($ch);
                $page = stristr($page, "<div class='signup-body'>");
                $action = 'https://api.twitter.com/oauth/authenticate';
                preg_match("/form action=\"(.*?)\"/", $page, $action);
                preg_match("/input name=\"authenticity_token\" type=\"hidden\" value=\"(.*?)\"/", $page, $authenticity_token);

                // -------------------------------------------------------
                // make login and get home page
                $strpost = "authenticity_token=".urlencode($token)."&username=".urlencode('username')."&password=".urlencode('password');
                //echo $action[1];
                curl_setopt($ch, CURLOPT_URL, $action);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost);
                $page = curl_exec($ch);
                // check if login was ok
                preg_match("/\<div class=\"warning\"\>(.*?)\<\/div\>/", $page, $warning);
                if (isset($warning[1])) return $warning[1];
                $page = stristr($page,"<div class='tweetbox'>");
                preg_match("/form action=\"(.*?)\"/", $page, $action);
                preg_match("/input name=\"authenticity_token\" type=\"hidden\" value=\"(.*?)\"/", $page, $authenticity_token);


                break;
            default:
                /* Show notification if something went wrong. */
                echo 'Could not connect to Twitter. Refresh the page or try again later.';
        }
		
		?>

its asking login again and again but not able to posting the message to twitter wall.

Please anybody give me solution.

Thanks,
Rajesh.


#2

It looks like your code is trying to do some things here that it absolutely should not do – you shouldn’t be doing any kind of form processing or otherwise automating the flow of acquiring user credentials whatsoever. To get access to a user’s account you MUST send them through an OAuth redirect flow. You should rethink your approach.