如何实现Twitter应用程序通过abraham / twitteroauth发推文

时间:2014-10-23 20:20:42

标签: php twitter twitter-oauth

我是oAuth,PHP和Twitter编程的新手。我正在尝试与Abraham Twitteroauth库开发一个Twitter应用程序。我已经从https://github.com/abraham/twitteroauth下载了源代码,并将其放在我的WAMP的www位置。 我的config.php是

<?php

/**
 * @file
 * A single location to store configuration.
 */

define('CONSUMER_KEY', 'nVD<hidden>');
define('CONSUMER_SECRET', 'DKTAW17fwK<hidden>');
define('OAUTH_CALLBACK', 'http://127.0.0.1/twitteroauth-master/twitteroauth-master/sdf.php');
?>

当我获得登录页面时,我非常确定Consumer密钥和密钥。 sdf.php是我用来发推文的文件

    <?php
    /**
     * @file
     * 
     */

    /* Load required lib files. */
    session_start();
    require_once('twitteroauth/twitteroauth.php');
    require_once('config.php');

    /* If access tokens are not available redirect to connect page. */
    if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
        header('Location: ./clearsessions.php');
    }
    /* Get user access tokens out of the session. */
    $access_token = $_SESSION['access_token'];

    /* Create a TwitterOauth object with consumer/user tokens. */
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

    /* If method is set change API call made. Test is called by default. */
    $content = $connection->get('account/rate_limit_status');
    echo "Current API hits remaining: {$content->remaining_hits}.";

    /* Get logged in user to help with tests. */
    $user = $connection->get('account/verify_credentials');

    $active = FALSE;
    if (empty($active) || empty($_GET['confirmed']) || $_GET['confirmed'] !== 'TRUE') {
      echo '<h1>Warning! This page will make many requests to Twitter.</h1>';
      echo '<h3>Performing these test might max out your rate limit.</h3>';
      echo '<h3>Statuses/DMs will be created and deleted. Accounts will be un/followed.</h3>';
      echo '<h3>Profile information/design will be changed.</h3>';
      echo '<h2>USE A DEV ACCOUNT!</h2>';
      echo '<h4>Before use you must set $active = TRUE in test.php</h4>';
      echo '<a href="./test.php?confirmed=TRUE">Continue</a> or <a href="./index.php">go back</a>.';
      exit;
    }

    function twitteroauth_row($method, $response, $http_code, $parameters = '') {
      echo '<tr>';
      echo "<td><b>{$method}</b></td>";
      switch ($http_code) {
        case '200':
        case '304':
          $color = 'green';
          break;
        case '400':
        case '401':
        case '403':
        case '404':
        case '406':
          $color = 'red';
          break;
        case '500':
        case '502':
        case '503':
          $color = 'orange';
          break;
        default:
          $color = 'grey';
      }
      echo "<td style='background: {$color};'>{$http_code}</td>";
      if (!is_string($response)) {
        $response = print_r($response, TRUE);
      }
      if (!is_string($parameters)) {
        $parameters = print_r($parameters, TRUE);
      }
      echo '<td>', strlen($response), '</td>';
      echo '<td>', $parameters, '</td>';
      echo '</tr><tr>';
      echo '<td colspan="4">', substr($response, 0, 400), '...</td>';
      echo '</tr>';

    }

    function twitteroauth_header($header) {
      echo '<tr><th colspan="4" style="background: grey;">', $header, '</th></tr>';
    }

    date_default_timezone_set('GMT');
$parameters = array('status' => date(DATE_RFC822));
$status = $connection->post('statuses/update', $parameters);
twitteroauth_row('statuses/update', $status, $connection->http_code, $parameters);

    ?>

我从test.php复制粘贴这一行。 我遇到的问题是,当我进入主页登录时,它再次将我重定向到connect.php。似乎它没有保存任何会话。请帮助我解决这个问题。

0 个答案:

没有答案