簡體   English   中英

帶推特后退按鈕的PHP Twitter搜索API

[英]PHP Twitter search api with tweet back button

我正在使用構建一個使用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>";

}

?>

我的問題:如何為每個結果集成一個推文按鈕-這將允許管理員(在Twitter搜索關鍵字匹配之后)在對話上發推文。

請參見以下示例: http : //screencast.com/t/2xBkTyUHT

找到推特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意圖

注意:您將echo "<div id='tweets'>"; 在一個循環中。 意味着DOM將具有多個具有相同ID的元素。 使用class糾正它或將其放在循環之外。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM