简体   繁体   English

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

[英]how do i display twitter tweets from twitter using php

i want to display twitter tweets in my webpage from twitter using php. 我想使用php在Twitter上的网页中显示Twitter推文。 anyone have idea help me 有人有主意帮我

thanks in advance. 提前致谢。

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

In addition to the libraries listed at the Twitter Developer Pages, you can use Zend_Service_Twitter to work with the Twitter API: 除了Twitter开发人员页面上列出的库之外,您还可以使用Zend_Service_Twitter与Twitter API一起使用:

Zend_Service_Twitter provides a client for the » Twitter REST API. Zend_Service_Twitter提供»Twitter REST API的客户端。 Zend_Service_Twitter allows you to query the public timeline. Zend_Service_Twitter允许您查询公共时间轴。 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. 如果您提供Twitter的用户名和OAuth详细信息,它将允许您获取和更新状态,回复朋友,直接发送消息的朋友,将推文标记为收藏,等等。

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

You could learn from Dabr , which is a PHP-frontend written in PHP. 您可以向Dabr学习,这是一个用PHP编写的PHP前端。 It has almost all the features of the Twitter API included. 它几乎包含了Twitter API的所有功能。

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. 它基于JavaScript,因此显然您不会遇到每小时API调用限制的问题。 And it gives you markup that you can easily modify however you like. 它为您提供了标记,您可以根据自己的喜好轻松地对其进行修改。

http://twitter.com/widgets/html_widget 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. 我没有在该站点的任何地方读过指南,您所质疑的问题必须进行重大改进才能打动所有人。 ;-) ;-)

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. 好的,所以我碰到了这篇文章,并为答案而苦苦挣扎...但这是我的解决方案...工作正常...我看到的唯一问题是基于获取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;
}

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. 我不是一个特别出色的程序员,因此,如果我犯了公然的错误或可以改进此脚本,我将不胜感激。

The Twitter API is changing and display requirements are no longer optional. Twitter API正在更改,显示要求不再是可选的。 So in addition to requiring the use of Oauth now, you should also be meeting these display standards as they are no longer optional. 因此,除了现在要求使用Oauth之外,您还应该满足这些显示标准,因为它们不再是可选的。

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. 有一些PHP库可帮助访问Twitter API的1.1版,我选择使用CodeBird,而不是继续自己开发。 I think the documentation could be a little better though. 我认为文档可能会更好一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM