我如何使用 php 显示来自 twitter 的 twitter 推文

发布于 2024-09-25 13:29:00 字数 71 浏览 3 评论 0原文

我想使用 php 在我的网页中显示来自 twitter 的 twitter 推文。任何人有想法帮助我

提前致谢。

i want to display twitter tweets in my webpage from twitter using php. anyone have idea help me

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

飘过的浮云 2024-10-02 13:29:00

您好,请查看 twitter API: http://dev.twitter.com/pages/libraries #php

Hello please take a look at the twitter API: http://dev.twitter.com/pages/libraries#php

天涯离梦残月幽梦 2024-10-02 13:29:00

除了 Twitter 开发人员页面上列出的库之外,您还可以使用 Zend_Service_Twitter 使用 Twitter API:

Zend_Service_Twitter 为 » Twitter REST API 提供客户端。 Zend_Service_Twitter 允许您查询公共时间线。如果您提供 Twitter 的用户名和 OAuth 详细信息,它将允许您获取和更新您的状态、回复朋友、直接向朋友发送消息、将推文标记为收藏夹等等。

In addition to the libraries listed at the Twitter Developer Pages, you can use Zend_Service_Twitter to work with the Twitter API:

Zend_Service_Twitter provides a client for the » Twitter REST API. Zend_Service_Twitter allows you to query the public timeline. If you provide a username and OAuth details for Twitter, it will allow you to get and update your status, reply to friends, direct message friends, mark tweets as favorite, and much more.

贩梦商人 2024-10-02 13:29:00

如果您想要一个真正简单的解决方案,您甚至可以使用 twitter 小部件: http://twitter.com/goodies/widgets

If you want a real easy solution, you could even grab the twitter widget: http://twitter.com/goodies/widgets

橘虞初梦 2024-10-02 13:29:00

您可以向 Dabr 学习,它是一个用 PHP 编写的 PHP 前端。它几乎包含了 Twitter API 的所有功能。

You could learn from Dabr, which is a PHP-frontend written in PHP. It has almost all the features of the Twitter API included.

风尘浪孓 2024-10-02 13:29:00

这是我发现的基本功能最好的一个。它基于 javascript,因此显然您不会遇到 API 每小时调用限制的问题。它为您提供了可以轻松修改的标记。

http://twitter.com/widgets/html_widget

哦,不用担心所有批评你问题的人。我没有在该网站的任何指南中读到您的问题必须非常先进才能给每个人留下深刻的印象。 ;-)

This is the best one I've found for basic functionality. It's javascript based, so apparently you're not going to run into issues with the API calls-per-hour limits. And it gives you markup that you can easily modify however you like.

http://twitter.com/widgets/html_widget

Oh, and don't worry about all the people criticizing your question. I didn't read in the guidelines anywhere for this site that you're question has to be significantly advanced to impress everyone. ;-)

后知后觉 2024-10-02 13:29:00

好吧,我遇到了这篇文章,并努力寻找答案...但这是我的解决方案...工作完美...我看到的唯一问题是基于检索 RSS 提要,Twitter 非常渴望获得该提要摆脱 - 但对于一个简单的解决方案来说,它很有魅力。

function twitter_status(){

$twitter_name = "YOUR_TWITTER_USERNAME";
$myFile = "http://api.twitter.com/1/statuses/user_timeline.rssscreen_name=".$twitter_name;

$dom = new DOMDocument();
$dom -> load($myFile);

$items = $dom->getElementsByTagName('item');

$max_items = 1; // Number of tweets to return.
$count = 0;

foreach ($items as $item) {
    // Select all the elements in the XML document named "Description"
    // The different elements available are Title, Description, pubDate, guid, link and twitter:source
    // You can find this out by opening the link to your twitter rss feed

    $tweets = $item->getElementsByTagName('description');
    $tweet_string = $tweets->item(0)->nodeValue;
    $tweet_string = substr($tweet_string,strpos($tweet_string,":")+2);


    $tweet_date = $item->getElementsByTagName('pubDate');
    $tweet_date = $tweet_date->item(0)->nodeValue;
    $tweet_date = substr($tweet_date,0,16); // Get rid of the excess times at the end of the date

    echo ("<li class='timestamp tweet_".$count."'>Posted ".$tweet_date."</li><li class='tweet tweet_".$count."'>".makelink($tweet_string)."</li>");

    $count = $count+1;
    if ($count>=$max_items){ break; }
    }
}

function makeLink($string){
// Function to convert url to a link

    /*** make sure there is an http:// on all URLs ***/
    $string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);

    /*** make all URLs links ***/
    $string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);

    /*** make all emails hot links ***/
    $string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);

    return $string;
}

这就是我的解决方案,但是它非常具体于我想要的 - 但你们中的大多数人都可以希望解决这个问题并进行必要的调整。我不是一个特别出色的编码员,所以如果我犯了明显的错误或者可以改进这个脚本,我将不胜感激。

Ok so I ran into this post and struggled with the answers... but here is my solution... works perfectly... the only issue I see is that is based around retrieving the RSS feed, which Twitter is pretty keen to get rid of - but for a simple solution it works a charm.

function twitter_status(){

$twitter_name = "YOUR_TWITTER_USERNAME";
$myFile = "http://api.twitter.com/1/statuses/user_timeline.rssscreen_name=".$twitter_name;

$dom = new DOMDocument();
$dom -> load($myFile);

$items = $dom->getElementsByTagName('item');

$max_items = 1; // Number of tweets to return.
$count = 0;

foreach ($items as $item) {
    // Select all the elements in the XML document named "Description"
    // The different elements available are Title, Description, pubDate, guid, link and twitter:source
    // You can find this out by opening the link to your twitter rss feed

    $tweets = $item->getElementsByTagName('description');
    $tweet_string = $tweets->item(0)->nodeValue;
    $tweet_string = substr($tweet_string,strpos($tweet_string,":")+2);


    $tweet_date = $item->getElementsByTagName('pubDate');
    $tweet_date = $tweet_date->item(0)->nodeValue;
    $tweet_date = substr($tweet_date,0,16); // Get rid of the excess times at the end of the date

    echo ("<li class='timestamp tweet_".$count."'>Posted ".$tweet_date."</li><li class='tweet tweet_".$count."'>".makelink($tweet_string)."</li>");

    $count = $count+1;
    if ($count>=$max_items){ break; }
    }
}

function makeLink($string){
// Function to convert url to a link

    /*** make sure there is an http:// on all URLs ***/
    $string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$string);

    /*** make all URLs links ***/
    $string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);

    /*** make all emails hot links ***/
    $string = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<A HREF=\"mailto:$1\">$1</A>",$string);

    return $string;
}

So this is my solution, however it's quite specific to what I wanted - but most of you can work this out hopefully and make necessary adjustments. I'm not a particularly great coder, so if I've made blatant mistakes or could improve this script I would appreciate it.

一抹苦笑 2024-10-02 13:29:00

Twitter API 正在发生变化,显示要求不再是可选的。因此,除了现在要求使用 Oauth 之外,您还应该满足这些显示标准因为它们不再是可选的。

有一些 PHP 库可以帮助访问 Twitter API 1.1 版本,我选择使用 CodeBird 而不是继续推进我自己的前进。我认为文档可能会更好一些。

The Twitter API is changing and display requirements are no longer optional. So in addition to requiring the use of Oauth now, you should also be meeting these display standards as they are no longer optional.

There are PHP libraries that help access version 1.1 of the Twitter API, I chose to use CodeBird rather than continue to roll my own going forward. I think the documentation could be a little better though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文