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

使用特定于用户名的php编写twitter API,php,twitter,Php,Twitter,我使用以下教程编写了一个twitter api应用程序: 如何修改脚本以生成特定于用户的时间轴流,以便应用程序在运行时显示用户的时间轴流而不是我的时间轴流(因为我编写了应用程序,因此它具有我的twitter凭据) 谢谢 php应用程序使用以下内容验证我的twitter凭据: <?php require 'tmhOAuth.php'; // Get it from: https://github.com/themattharris/tmhOAuth // Use the data fro

我使用以下教程编写了一个twitter api应用程序:

如何修改脚本以生成特定于用户的时间轴流,以便应用程序在运行时显示用户的时间轴流而不是我的时间轴流(因为我编写了应用程序,因此它具有我的twitter凭据)

谢谢

php应用程序使用以下内容验证我的twitter凭据:

<?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>\n";
    echo "Error: ",$connection->response['error'], "<br>\n";

您可以向以下url发送get请求以获取用户时间线

 https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
您可以将参数屏幕名称替换为您想要访问的用户名,也可以将count替换为您想要获得的tweet数量,count是可选的,不必包括在内


您可以在office twitter API网站上阅读更多关于状态/用户时间线的信息:

如果您希望让用户登录,那么最好使用

下载并包含在项目中,然后包含库并启动会话

require("twitteroauth/twitteroauth.php");  
session_start();  
然后创建一个新实例并使用应用程序详细信息进行身份验证。您可以设置在用户进行身份验证时重定向到的url。您还需要缓存令牌

$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'];  
将用户重定向到twitter以进行身份验证

header('Location: '.$twitteroauth->getAuthorizeURL($request_token['oauth_token']));
在设置twitter重定向到的文件中,需要使用创建的令牌重新验证。Twitter还将向url添加一个参数,您可以使用该参数为该用户创建访问令牌。现在,当您向twitter发送GET请求时,它会代表登录的用户执行此操作

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);
您可以从
$user_info
获取更多详细信息,您可以将其缓存或存储在数据库中,这样您就可以记住已通过身份验证的用户。您将需要使用
oauth\u令牌
oauth\u秘密
,类似这样的东西

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

展示一些代码怎么样?我看不出有多少人去看视频只是为了猜测你的代码可能是什么样子。我很确定你不能查看其他用户的时间表。这不是它的工作原理。你知道,隐私等等。当用户使用你的应用程序时,他们会给它提供他们的凭据,这样他们就可以看到自己的东西。抱歉,我在赶时间@Novocaine88查看上面更新的问题。我认为你不能只添加一段代码来实现你想要的。您的代码需要更加复杂,以便向其他用户显示时间线。请参阅上面更新的问题。我想添加一段代码,要求用户登录,然后api调用是否会提取该用户的身份验证凭据?