简体   繁体   中英

PHP Twitter search api with tweet back button

I'm using working on building a WordPress plugin, that uses the Twitter api - but I'm very new to working with Twitter.

I currently have a keyword search form and results working

<?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>";

}

?>

My question: How can I integrate a tweet button for each result - this would allow the admin to (after searching twitter for keyword matches) tweet back on conversations.

Please see this example: http://screencast.com/t/2xBkTyUHT

Find the tweet id then attach a hyper link of this format

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

So the link might look like this.

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

More information can be found here. Twitter Web Intents

Note: You put echo "<div id='tweets'>"; in a loop. Means DOM will have multiple elements with same id. Correct it either using class or put it outside the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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