Sending a tweet with oauth-php?

2019-08-08 21:22发布

<?php
include_once("twitter/library/OAuthStore.php");
include_once("twitter/library/OAuthRequester.php");
// register at http://twitter.com/oauth_clients and fill these two 
define("TWITTER_CONSUMER_KEY", "hidden");
define("TWITTER_CONSUMER_SECRET", "hidden");

define("TWITTER_OAUTH_HOST","https://api.twitter.com");
define("TWITTER_REQUEST_TOKEN_URL", TWITTER_OAUTH_HOST . "/oauth/request_token");
define("TWITTER_AUTHORIZE_URL", TWITTER_OAUTH_HOST . "/oauth/authorize");
define("TWITTER_ACCESS_TOKEN_URL", TWITTER_OAUTH_HOST . "/oauth/access_token");
define("TWITTER_PUBLIC_TIMELINE_API", TWITTER_OAUTH_HOST . "/statuses/public_timeline.json");
define("TWITTER_UPDATE_STATUS_API", TWITTER_OAUTH_HOST . "/statuses/update.json");

define('OAUTH_TMP_DIR', function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : realpath($_ENV["TMP"])); 

// Twitter test
$options = array('consumer_key' => TWITTER_CONSUMER_KEY, 'consumer_secret' => TWITTER_CONSUMER_SECRET);
OAuthStore::instance("2Leg", $options);

try
{
    $request = new OAuthRequester(TWITTER_REQUEST_TOKEN_URL, "POST");
    $result = $request->doRequest(0);
    parse_str($result['body'], $params);
    $request = new OAuthRequester(TWITTER_PUBLIC_TIMELINE_API, 'GET', $params);
    $result = $request->doRequest();
    parse_str($result['body'], $params);
}
catch(OAuthException2 $e)
{
    echo "Exception" . $e->getMessage();
}
?>

How do I send a tweet to twitter? It successfully does the request because it returns 0, however I still am not sure where to start if I wanted to send a tweet. I am the only one using this I just want to send a tweet from my site instead of logging in on twitter.

Thank you.

0条回答
登录 后发表回答