【问题标题】:PHP Twitter search api with tweet back buttonPHP Twitter 搜索 api 与推文返回按钮
【发布时间】:2012-10-26 10:20:02
【问题描述】:

我正在使用构建一个使用 Twitter api 的 WordPress 插件 - 但我对使用 Twitter 非常陌生。

我目前有一个关键字搜索表单和结果工作

<?php

// get the keyword from url
$srchterm = $_GET['search_box'];

// encode it for the safe search
$srchterm = urlencode($srchterm);

// search the keyword using Twitter API
$srch_twitts = "http://search.twitter.com/search.atom?q=".$srchterm."";

// use curl to execute the Twitter URL
$twits = curl_init();
curl_setopt($twits, CURLOPT_URL, $srch_twitts);
curl_setopt($twits, CURLOPT_RETURNTRANSFER, TRUE);
$twi = curl_exec($twits);

// here we have the searched result in an array
$search_res = new SimpleXMLElement($twi);

//print_r($search_res);

?>
<?php

/* display the data */

$i = 0;

// displays the search keyword
echo "<p style='padding-left:10px; color:brown'>Your search term is: " . stripslashes($_GET['q']) . "</p>";

// tweets in an array. split it by foreach
// we need only 10 result so, use if condition
foreach ($search_res->entry as $result) if ($i++ < 10)
{

echo "<div id='tweets'>";
echo "<p>$i</p> ";
echo "<div class='avatar'><img src='". $result->link[1]->attributes()->href. "' /> </div>";
echo "<div class='twitcont'>";
echo "<div class='name'>". $result->author->name . "</div>";
echo "<div class='content'>" . $result->content ;

// convert the updated date of tweet to seconds
$twittedSeconds = time() - strtotime($result->updated);
$names = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
$seconds = array( 1,        60,       3600,   86400, 604800, 2630880, 31570560);

// find the time difference between present time and tweet time

if($twittedSeconds > 0)
{
for($j = count($seconds) - 1; $j >= 0 ; $j--)
{
$interval = $seconds[$j];
if($twittedSeconds >= $interval)
{
$getTime =  round($twittedSeconds / $interval);
if ($getTime > 1)
{
$names[$j] .= s;
}
$time = $getTime. ' ' . $names[$j] . ' ago';
break;
}
}
//echo the time difference
echo "<div class='time'> " . $time . "</div>";
}
echo "</div>";
echo "</div></div>";

}

?>

我的问题: 我如何为每个结果集成一个推文按钮 - 这将允许管理员(在搜索推特以查找关键字匹配后)回推对话。

请看这个例子:http://screencast.com/t/2xBkTyUHT

【问题讨论】:

  • 这个后退按钮有什么作用? retweetreply ?
  • 是的,我做到了——非常感谢它完美地工作:我还有一个问题——如果有必要,我将如何添加“转推”按钮。 ——
  • 你读过this吗?也有转推 api。如果这对你有用。请接受答案。
  • 我从未听说过这个 Web Intents - 谢谢,我要去看看。我会将问题标记为已回答,您提供了很棒的帮助。再次感谢。

标签: php twitter


【解决方案1】:

找到推文ID,然后附上这种格式的超链接

https://twitter.com/intent/tweet?in_reply_to={tweet id}

所以链接可能看起来像这样。

$tweet_id = substr($entry->id, strrpos($entry->id, ':')+1);
echo "<a href=\"https://twitter.com/intent/tweet?in_reply_to={$tweet_id}\">Reply</a>";

更多信息可以在这里找到。 Twitter Web Intents

注意:您将echo "&lt;div id='tweets'&gt;"; 放在一个循环中。意味着 DOM 将有多个具有相同 id 的元素。使用class 更正它或将其置于循环之外。

【讨论】:

  • 我还有一个问题 - 如果有必要,我该如何添加“转推”按钮。
  • 刚刚注意到这条推文只会发送到我的帐户(而不是推文 ID)
  • 当你回复时,你会写一些反对推文的东西。因此需要 tweet id。它是你的回复,所以它实际上是你的推文,但前面有@user,并且会有一个in reply to 链接。构建链接需要tweet id
  • 推特 ID 未显示在网址中 - 这是我点击回复时显示的内容:twitter.com/intent/tweet?in_reply_to=&original_referer=reply_to={$tweet_id}(网址中为空)
猜你喜欢
  • 2015-12-10
  • 2018-09-23
  • 2018-09-15
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
相关资源
最近更新 更多