Twitter API 1.1 - 渲染twitter的t.co链接

时间:2013-06-14 13:00:37

标签: twitter hyperlink

我想在我的网站上显示一个帐户的推文。问题是,推文始终采用格式http://t.co/...,而不是我想要的完整链接。

例如,我获得:

the rules of the game are all implemented - local players can play together in this link: http://t.co/Nf7j4TaB

if you are very curious... then, here is the link to the xodul's section under development: http://t.co/6Zbti36T

etc...

我希望这些推文看起来像这样:

the rules of the game are all implemented - local players can play together in this link: http://xodul.com/tests/js/

if you are very curious... then, here is the link to the xodul's section under development: http://xodul.com/tests 

etc...

为了完成我的申请,我遵循了以下说明:

Simplest PHP example for retrieving user_timeline with Twitter API version 1.1(从这里我们可以获得每条推文的文字,链接的格式为:http://t.co/...

Rendering links in tweet when using Get Statuses API 1.1(得分最高的答案的代码,在此链接中替换了带有超链接"http://t.co/Nf7j4TaB"的文本"<a target='_blank' href='http://t.co/Nf7j4TaB'>http://t.co/Nf7j4TaB</a>"

我非常感谢有关如何呈现Twitter链接的任何帮助!

3 个答案:

答案 0 :(得分:0)

通过您所遵循的教程,您可以使用这些属性来显示实际链接。

Note: In API v1.1, entities will always be included unless you set include_entities to False or 0.

The urls entity

An array of URLs extracted from the Tweet text. Each URL entity comes with the following attributes:

url:    The URL that was extracted
display_url:    (only for t.co links) Not a URL but a string to display instead of the URL
expanded_url:   (only for t.co links) The fully resolved URL
indices:    The character positions the URL was extracted from

https://dev.twitter.com/docs/tweet-entities

答案 1 :(得分:0)

目前仅限JavaScript解决方案,无需使用新的1.1 API即可在您的网站上发布Twitter帖子并实际返回帖子中的完整网址,而非Twitter缩短版本:-) http://goo.gl/JinwJ

答案 2 :(得分:0)

感谢您的回答。

在建议的链接(https://dev.twitter.com/docs/tweet-entities)中分析了JSON后,我为暴露的问题编写了一个解决方案:

// ...
$twitter_data = json_decode($json);    // last line of the code in: http://stackoverflow.com/questions/12916539


// print the tweets, with the full URLs:
foreach ($twitter_data as $item) {
    $text = $item->text;

    foreach ($item->entities->urls as $url) {
        $text = str_replace($url->url, $url->expanded_url, $text);
    }
    echo $text . '<br /><br />';

    // optionally, here, the code from: http://stackoverflow.com/questions/15610968/
    // can be added, too.
}