protected function authTwitter($save_name) { $config = $this->config; $conn = new TwitterOAuth($config['consumer_key'], $config['consumer_secret']); $request_token = $conn->getRequestToken(); if ($request_token === false || $conn->lastStatusCode() != 200) { throw new RuntimeException("Error fetching Twitter auth token: Status code " . $conn->lastStatusCode() . '; ' . $conn->http_error); } $url = $conn->getAuthorizeURL($request_token); // Automatically send the user to the auth page. $ui = new UI(); $ui->openBrowser($url); $pin = $ui->readline("Please visit {$url} then type the pin number: "); $conn = new TwitterOAuth($config['consumer_key'], $config['consumer_secret'], $request_token['oauth_token'], $request_token['oauth_token_secret']); $access_token = $conn->getAccessToken($pin); if ($access_token === false || $conn->lastStatusCode() != 200) { throw new RuntimeException("Error fetching Twitter access token: Status code " . $conn->lastStatusCode() . '; ' . $conn->http_error); } $this->config['oauth_token'] = $access_token['oauth_token']; $this->config['oauth_token_secret'] = $access_token['oauth_token_secret']; echo "Your Twitter token is " . $access_token['oauth_token'] . "\n"; echo "Your Twitter token secret is " . $access_token['oauth_token_secret'] . "\n"; echo "(Written to twitter-{$save_name}.txt)\n"; if (file_put_contents("twitter-{$save_name}.txt", $access_token['oauth_token'] . "\n" . $access_token['oauth_token_secret'])) { return; } throw new RuntimeException("Failed to save oauth token to twitter-{$save_name}.txt"); }
/** * Sends a tweet * * @param string $txt the tweet text to send * @param bool $limit DEPRECATED * * @return string URL of tweet (or false on failure) */ public function sendTweet($txt, $limit = false) { if (!$this->authenticatedAsUser) { return false; } $resp = $this->api->post('statuses/update', array('status' => $txt)); if (200 != $this->api->lastStatusCode()) { return false; } return $resp; }