简体   繁体   English

用PHP获取最后一条推文

[英]Getting last tweet with PHP

To get my last tweet in PHP I use this code : 要用PHP获取我的最后一条推文,我使用以下代码:

function getTweets($tweetsToDisplay, $user_id) { 

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?user_id=' . $user_id . '&include_rts=true&count=' . $tweetsToDisplay; 
$twitterci = curl_init($twitterrequest); 
curl_setopt($twitterci, CURLOPT_RETURNTRANSFER, TRUE); 
$twitterinput = curl_exec($twitterci); 
curl_close($twitterci); 
return ($twitterinput); 

} 

$user_id = '99999999';
$var = json_decode(getTweets(1, $user_id));
$txt = $var[0]->text;
$txt = preg_replace('%(https?://)[^\s]*%', '$1...', $txt);
echo $txt;

Works fine but I want to get the date as well. 工作正常,但我也希望得到日​​期。 How to extract it ? 如何提取它?

I hope code below help you. 我希望下面的代码可以帮到你。

function getTimeline($count, $username) {
   $url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.$username.'&count=.'$count;
   $tweets = json_decode(file_get_contents($url),TRUE);

   return $tweets;
}

You can try this simple PHP function that I created to catch latest tweets easily (does not require API authentication). 您可以尝试我创建的这个简单的PHP函数来轻松捕获最新的推文(不需要API身份验证)。 Must be optimized :) 必须优化:)

function getTweets($user, $count) {
    $datas = file_get_contents('https://twitter.com/'.$user);

    preg_match_all('/<p class="js-tweet-text tweet-text">(.*?)<\/p>/', $datas, $matchetweets);
    $matchetweets[1] = preg_replace('/<s>(.?)<\/s>/', '$1', $matchetweets[1]);
    $matchetweets[1] = preg_replace('/(class|dir|rel|data-expanded-url|data-pre-embedded|data-query-source)="(.*?)"/', '', $matchetweets[1]);
    $matchetweets[1] = preg_replace('!\s+!', ' ', $matchetweets[1]);

    for ($i = 1; $i <= $count; $i++) {
        echo '<li>'.$matchetweets[1][$i].'</li>'."\n";
    }
};

Usage 用法

echo getTweets('nasa', 3);

UPDATE (10/15/2014) : 更新(2014年10月15日):

This version is out of date and not working anymore. 此版本已过期,不再有效。 Here is an updated PHP code to parse tweets easily. 这是一个更新的PHP代码,可以轻松解析推文。

function getTweets($user, $count) {
    $datas = file_get_contents('https://mobile.twitter.com/'.$user);

    preg_match_all('/<div class="tweet-text" data-id="\d*">(.*?)<\/div>/', $datas, $matchetweets);
    $matchetweets[1] = preg_replace('/<div class="dir-ltr" dir="ltr">/', '', $matchetweets[1]);

    for ($i = 1; $i <= $count; $i++) {
        echo '<li>'.$matchetweets[1][$i].'</li>'."\n";
    }
};

UPDATE (5/30/2015) : 更新(2015年5月30日):

function getTweets($user, $count) {
    $datas = file_get_contents('https://mobile.twitter.com/'.$user);

    preg_match_all('/<div class="tweet-text" data-id="\d*">(.*?)<\/div>/s', $datas, $matchetweets, PREG_SET_ORDER);

    for ($i = 1; $i <= $count; $i++) {
        $matchetweets[$i][0] = preg_replace('/<div class="dir-ltr" dir="ltr">/', '', $matchetweets[$i][0]);
        $matchetweets[$i][0] = preg_replace('/\s+/', ' ', $matchetweets[$i][0]);
        $matchetweets[$i][0] = str_replace('"> ', '">', $matchetweets[$i][0]);

        echo '<li>'.$matchetweets[$i][0].'</li>'."\n";
    }
};

Usage doesn't change. 用法不会改变。 Minimum 1 tweet, maximum 20 tweets. 最少1条推文,最多20条推文。

<?php
$date = $var[0]->created_at;

That should work! 这应该工作!

Add this to bottom of your code. 将其添加到代码的底部。

$time = $var[0]->created_at;
echo $time;

Based on the Twitter API results from http://dev.twitter.com/doc/get/statuses/user_timeline , it appears that you could use the created_at param. 基于来自http://dev.twitter.com/doc/get/statuses/user_timeline的Twitter API结果,您似乎可以使用created_at参数。

You have: 你有:

$txt = $var[0]->text;

If that works then add 如果有效则再添加

$created = $vars[0]->created_at;

echo $txt;
echo "<span>".$created."</span>" ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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