1

我正在使用 twitter API 来获取我想通过我的模板输出的时间线。我得到这样的提要:

public static function getTwitterFeed(){
    $settings = array(
        'oauth_access_token' => "xxx",
        'oauth_access_token_secret' => "xxx",
        'consumer_key' => "xxx",
        'consumer_secret' => "xxx"
    );

    $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
    $getfield = '?screen_name=xxx&count=5';
    $requestMethod = 'GET';
    $twitter = new TwitterAPIExchange($settings);
    $returnTwitter = $twitter->setGetfield($getfield)
                 ->buildOauth($url, $requestMethod)
                 ->performRequest();
    return json_decode($returnTwitter);

}

这将返回一个对象数组(推文是对象),我希望能够在我的模板中循环遍历它,如下所示:

<% loop TwitterFeed %>
    <h4>$created_at</h4>
    <p>$text</p>
<% end_loop %>

正如我上面所说的,循环输入一次,但没有识别出任何值。我怎样才能做到这一点?

4

2 回答 2

4

DataObjects在 SilverStripe 中表示来自数据库的记录,在您的情况下,您使用ArrayData.
使用$array = Convert::json2array($returnTwitter)$array = json_decode($returnTwitter, true)代替。
并查看https://stackoverflow.com/a/17922260/1119263了解如何使用ArrayData

于 2013-11-10T10:52:51.307 回答
1

感谢 Zauberfisch 为我指明了正确的方向。我这样解决了:

public static function getTwitterFeed(){
    $settings = array(
        'oauth_access_token' => "xxx",
        'oauth_access_token_secret' => "xxx",
        'consumer_key' => "xxx",
        'consumer_secret' => "xxx"
    );

    $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
    $getfield = '?screen_name=xxx&count=5';
    $requestMethod = 'GET';
    $twitter = new TwitterAPIExchange($settings);
    $returnTwitter = $twitter->setGetfield($getfield)
                 ->buildOauth($url, $requestMethod)
                 ->performRequest();

    $returnTwitter = Convert::json2array($returnTwitter);

            $tweets = array();
            foreach ($returnTwitter as $key => $value) {
                $tweets[] = new ArrayData(array('created_at' => $value['created_at'], 'text' => $value['text']));


            }
                return new ArrayList($tweets);

    }
于 2013-11-10T12:11:41.150 回答