使用特定于用户名的php编写twitter API

I've written a twitter api application using the following tutorial:

http://www.youtube.com/watch?v=GQaPt-gQVRI

How can I modify the script to generate a timeline stream that is specific to a user so that the application when run will show user's timeline stream and not mine (since i wrote the app and therefore it has my twitter credentials)

Thanks

the php application validates my twitter credentials using the following:

<?php
require 'tmhOAuth.php'; // Get it from: https://github.com/themattharris/tmhOAuth

// Use the data from http://dev.twitter.com/apps to fill out this info
// notice the slight name difference in the last two items)

$connection = new tmhOAuth(array(
  'consumer_key' => 'my key',
    'consumer_secret' => 'my secret',
    'user_token' => 'my token', //access token 
    'user_secret' => 'my user secret' //access token secret
));

// set up parameters to pass
$parameters = array();

if ($_GET['count']) {
    $parameters['count'] = strip_tags($_GET['count']);
}

if ($_GET['screen_name']) {
    $parameters['screen_name'] = strip_tags($_GET['screen_name']);
}

if ($_GET['twitter_path']) { $twitter_path = $_GET['twitter_path']; }  else {
    $twitter_path = '1.1/statuses/user_timeline.json';
}

$http_code = $connection->request('GET', $connection->url($twitter_path), $parameters );

if ($http_code === 200) { // if everything's good
    $response = strip_tags($connection->response['response']);

    if ($_GET['callback']) { // if we ask for a jsonp callback function
        echo $_GET['callback'],'(', $response,');';
    } else {
        echo $response; 
    }
} else {
    echo "Error ID: ",$http_code, "<br>
";
    echo "Error: ",$connection->response['error'], "<br>
";

So without having to pass a new username in the api call, how can i add a snippet to require the user to log in? and if i add that snippet for the user to log in, will the api automatically populate the authentication strings with the user's?

You can send a get request to the following url to get a users timeline.

 https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2

You can replace the parameters screen_name with the username you want to access, and you can replace count with the number of tweets you would like to get, count is optional and doesn't have to be included.

You can read more about statuses/user_timeline on the office twitter API site: https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline

If you wish to get a user to sign in then your best bet would be to use the twitteroauth library by abraham

Download and include in your project, then include the library and start a session.

require("twitteroauth/twitteroauth.php");  
session_start();  

Then create a new instance and authenticate with your app details. You can set a url to redirect to when the user authenticates. You also need to cache your tokens.

$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');  
$request_token = $twitteroauth->getRequestToken('http://example.com/loggedin.php');

$_SESSION['oauth_token'] = $request_token['oauth_token'];  
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];  

Redirect the user to twitter to authenticate

header('Location: '.$twitteroauth->getAuthorizeURL($request_token['oauth_token']));

In the file that you set twitter to redirect to you need to re-authenticate using the tokens created. Twitter will also add a parameter to your url which you use to create a access token for that user. Now when you send GET requests to twitter, it does it on behalf of the user logged in.

require("twitteroauth/twitteroauth.php");  
session_start();  

$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET', $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);  

$user_info = $twitteroauth->get('account/verify_credentials'); 
print_r($user_info);

You can get additional details from $user_info which you can cache or store in a database, which will allow you to remember users that have already authenticated. You will need to use oauth_token and oauth_secret, something like this.

$twitteroauth = new TwitterOAuth('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET', 'OAUTH_TOKEN', 'OAUTH_SECRET');