我正在使用构建一个使用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>";
}
?>
我的问题: 如何为每个结果集成一个推文按钮 - 这将允许管理员(在搜索推特上的关键字匹配后)回复对话。
答案 0 :(得分: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 "<div id='tweets'>";
置于循环中。意味着DOM将具有多个具有相同id的元素。使用class
纠正它或将其放在循环外。