使用 Twitter 流 API
我需要使用实时推文样本。我有这段代码,但我无法通过调用正确的链接得到任何信息。当然,我输入了正确的用户名和密码,但仍然没有成功。请指教是因为 Twitter 最近更改了 API。抱歉,如果这个问题很基本,我对此很陌生。提前致谢
<?php
//datacollector.php
$fp =
fopen("http://username:[email protected]/spritzer.json
while($data = fgets($fp))
{
$time = date("YmdH");
if ($newTime!=$time)
{
@fclose($fp2);
$fp2 = fopen("{$time}.txt","a");
}
fputs($fp2,$data);
$newTime = $time;
}
?>
I need to consume the real time tweets sample. I have this piece of code but i could not get anything ma i calling the right link. definitely i put the right username and password but still it did not go through. Please advice is it because twitter changed the API recently. Sorry if the question is basic I am new to that. Thanks in advance
<?php
//datacollector.php
$fp =
fopen("http://username:[email protected]/spritzer.json
while($data = fgets($fp))
{
$time = date("YmdH");
if ($newTime!=$time)
{
@fclose($fp2);
$fp2 = fopen("{$time}.txt","a");
}
fputs($fp2,$data);
$newTime = $time;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
DWRoelands 发布的有关弃用的内容是正确的,但这实际上不是您的问题 - 问题是 Twitter 已弃用 spritzer.json。您应该使用
/1/statuses/sample.json
来获取消防软管。这是一个有效的命令行示例:如果您希望在生产中长期使用此代码,我当然会考虑实现 OAuth,但由于 Twitter 甚至还没有宣布关闭基本身份验证的时间表,因此没有如果你只是在闲逛,那就赶紧吧。当然,他们明天可以在没有任何警告的情况下将其关闭。
这是一些有效的 PHP 代码,只需将 username:password 替换为实际凭据:
What DWRoelands posts is true about deprecation, but that's not actually your problem -- the problem is that Twitter deprecated spritzer.json. You should instead use
/1/statuses/sample.json
to get the firehose. Here's a command-line example that works:If you're looking to use this code in production for a long time, I would certainly consider implementing OAuth, but since Twitter hasn't even announced a schedule for turning off Basic Auth, there's no rush if you're just fooling around. Of course, they could turn it off tomorrow without any warning.
Here's some working PHP code, just replace username:password with actual credentials:
如果您需要一个应用程序,您可以使用 Twitter C# API :Tweetinvi 使用 C# 轻松开发
该 API 提供了一个 Stream 类这将允许您使用委托函数检索流的信息。
以下是示例提供的示例:
实施起来既简单又快速。
我是该项目的开发人员之一。
If you need an application you can develop easily with C# using Twitter C# API : Tweetinvi
The API is providing a class Stream that will allow you to retrieve the information of the stream by using a delegate function.
Here is the example provided by the samples:
Easy and fast to implement.
I am one of the developer working on the project.
流 API 文档表明您需要使用 OAuth 进行验证才能访问用户流。请参阅:Twitter:流 API 概念
该页面还表明基本身份验证将被弃用,所以我认为您需要调整代码以使用 OAuth。
The streaming API documentation indicates that you need to validate with OAuth in order to access user streams. See: Twitter: Streaming API Concepts
That page also indicates that Basic Authentication is going to be deprecated, so I think you'll need to adapt your code to use OAuth.
从此处下载 lib 文件夹。将它们放入与以下代码相同的文件夹中。修改 Api 键并更改您要跟踪的关键字。如果您要在浏览器中执行它,请稍等一下。
Download lib folder from here.Put them inside the same folder with this code below. Modify Api keys and change keyword you want to track. If you gonna execute it in browser wait a bit.