Get latest Tweets - What API

血红的双手。 提交于 2021-02-11 07:20:28

问题


I know this has been discussed a lot here, but I still don't seem able to find aworking solution. Either it's out of date, it's the wrong programming-language or it's no longer supported.

All I want to do is: Get the last 'n' tweets from a public Twitter profile using PHP/JavaScript.

What API should I use best?
How do I keep it as lightweight as possible?
I can't use Node. js

I tried this, but I can't seem to get it to work, as simple as it may look like. Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

I've already registered for a developer account and created a "Twitter-App".


回答1:


You can use this API, which I have used once and it works perfectly.

Download the file TwitterApiExchange.php from above repository and you may use following sample code to fetch tweets of an user:

<?php
echo '<pre>';
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$user = "rootcss";
$count = 100;

if (isset($_GET['user'])) { $user = $_GET['user']; }
if (isset($_GET['count'])) { $count = $_GET['count'];}

$getfield = "?screen_name=$user&count=$count";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(),$assoc = TRUE);

if(isset($string["errors"][0]["message"])) {
    echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";
    exit();
}
foreach($string as $items)
    {
        echo "Time and Date of Tweet: ".$items['created_at']."<br />";
        echo "Tweet: ". $items['text']."<br />";
        echo "Tweeted by: ". $items['user']['name']."<br />";
        echo "Screen name: ". $items['user']['screen_name']."<br />";
        echo "Followers: ". $items['user']['followers_count']."<br />";
        echo "Friends: ". $items['user']['friends_count']."<br />";
        echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
    }

echo '</pre>';
?>

It worked perfectly for me, Let me know if you face any issue.



来源:https://stackoverflow.com/questions/33541839/get-latest-tweets-what-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!