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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里