php获取推特feed twitter timeline feed

1. https://apps.twitter.com/

申请 consumer_key, consumer_secret,oauth_access_token,oauth_access_token_secret

 

2. 下载项目:https://github.com/kmaida/twitter-timeline-php

下载:twitter-timeline-php-master

 

本人净化版:

require_once('_utils/twitter-api-oauth.php');

function formatTweet($tweet) 
{
    $linkified = '@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@';
    $hashified = '/(^|[\n\s])#([^\s"\t\n\r<:]*)/is';
    $mentionified = '/(^|[\n\s])@([^\s"\t\n\r<:]*)/is';
    
    $prettyTweet = preg_replace(
        array(
            $linkified,
            $hashified,
            $mentionified
        ), 
        array(
            '<a href="$1" class="link-tweet" target="_blank">$1</a>',
            '$1<a class="link-hashtag" href="https://twitter.com/search?q=%23$2&src=hash" target="_blank">#$2</a>',
            '$1<a class="link-mention" href="http://twitter.com/$2" target="_blank">@$2</a>'
        ), 
        $tweet
    );
    
    return $prettyTweet;
}
    
$settings = array(
    'consumer_key'              => "******",
    'consumer_secret'           => "******",
    'oauth_access_token'        => "******",
    'oauth_access_token_secret' => "******"
);

$tweetCount      = 3;
$twitterUsername = '';
$url             = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield        = '?screen_name=' . $twitterUsername . '&count=' . $tweetCount;

$twitter      = new TwitterAPITimeline($settings);
$json         = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
$twitter_data = json_decode($json, true);   // Create an array with the fetched JSON data

//echo '<pre>'; print_r($twitter_data) ;

$data = array();

foreach($twitter_data as $feed) 
{
    $photos = array();
    $media  = $feed['entities']['media'];
    if($media) foreach($media as $img) $photos[] = $img['media_url'];
    
    $data[] = array('tweet'=>formatTweet($feed['text']),'date'=>$feed['created_at'],'img'=>$photos);
}

echo '<pre>'; print_r($data);

 

justcode.ikeepstudying.com

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值