用于检索推文的代码段:
<?php
header('Content-type: application/json');
require "config.php"; //Containing consumer key and consumer secret
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$query = array(
"q" => "#CM0677",
"count" => 5,
"result_type" => "recent"
);
$tweets = $connection->get('search/tweets', $query);
echo(json_encode($tweets));
?>
通过JS附加到HTML的代码片段:
$(document).ready(function() {
//get the JSON data that is echoed out in the //call_twitter_API.php code
$.getJSON("call_twitter_API.php",function(tweetdata) {
$.each(tweetdata, function(i, tweet){
$("#tweet-list").append("<li>" + tweet.text +"</li>");
});
})
})
HTML文件包含ID为tweet-list的UL