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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度