douliexu5623 2013-08-01 19:02
浏览 27
已采纳

使用PHP中的JSON Twitter Feed输出和链接推文剥离元数据(1.1与OAuth)

I'm switching this twitter feed that I used to have to the new(er) v1.1 API that uses OAuth.

I got the feed server side using a Github project someone had done and then outputted it into a variable and echoed it. Only thing is that I'm not familiar with JSON.

require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library

$twitteruser = "goinnative";
// removed token and related variables for this example

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$feed = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

echo $feed;

That's great and all, but now I have two issues:

  1. There seems to be an error at the bottom of my JSON feed. It gives me a line number but no error information and it looks like a PHP error. Any ideas? Full paste of JSON here: http://pastebin.com/0Ba6cwY2

  2. Can I put this JSON output into an array or something and then strip everything but the actual tweet content and links?

  • 写回答

1条回答 默认 最新

  • dongze8698 2013-08-08 18:20
    关注

    Solved the issue. I didn't realize my feed was already in a PHP array. Therefore solving my issue was as simple as echoing all the [text] pockets of that array in a foreach loop.

    $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
    $tweetOutput="";
    foreach ($tweets as $tweets) {
            $tweetOutput .= $tweets->text."<span class='tweetSeperator'>"."&nbsp; &bull; &nbsp;"."</span>";
    }
    

    After outputting the tweet all I had to do was linkify (http's, mentions and hashtags):

    function linkifyTweet($statusText)
    {
      $statusText = preg_replace('/(https?:\/\/\S+)/', '<a href="\1">\1</a>', $statusText);
      $statusText = preg_replace('/(^|\s)@(\w+)/', '\1@<a href="http://twitter.com/\2">\2</a>', $statusText);
      $statusText = preg_replace('/(^|\s)#(\w+)/', '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', $statusText);
      return $statusText;
    }
    echo linkifyTweet($tweetOutput);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料