Twitter API 1.1 显示最新推文

问题描述 投票:0回答:1

我使用以下代码将我帐户的最新推文获取到我的 WordPress 网站。 Twitter一推出新的API 1.1,一切就彻底崩溃了。如何进行。

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$cvt_twitter );

if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity( 5 ); 

    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items( 0, $maxitems );

endif;
?>

<ul style="list-style:none;">
    <?php if ( $maxitems == 0 ) : ?>
        <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
    <?php else : ?>
        <?php // Loop through each feed item and display each item as a hyperlink. 
            foreach ( $rss_items as $item ) : ?>
            <li>
            <div class="row">
            <div class="span2">
                <a class="thumb" href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank">
                  <img width="100" height="auto" src="<?=$cvt_logo?>">
                </a>
            </div>
            <div class="span9">
                <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>" target="_blank">
                    <?php $string = esc_html( $item->get_title() );
                    $word = substr($string, 0, strpos($string, ':')+1);
                    echo str_replace($word, "", $string); ?>
                </a>
            </div></div>            
        </li>
        <?php endforeach;
        endif; ?>
</ul>
php wordpress twitter
1个回答
0
投票

尝试使用适用于 WordPress 的 Twitter API 1.1 客户端

它非常容易实现,您只需要您的 consumer_keyconsumer_secret

示例(来自自述文件):

<?php

// Include Twitter API Client
require_once( 'class-wp-twitter-api.php' );

// Set your personal data retrieved at https://dev.twitter.com/apps
$credentials = array(
  'consumer_key' => 'xxxxxxxxxxxxxxxx',
  'consumer_secret' => 'xxxxxxxxxxxxxxxx'
);

// Let's instantiate Wp_Twitter_Api with your credentials
$twitter_api = new Wp_Twitter_Api( $credentials );

// Example a - Retrieve last 5 tweets from my timeline (default type statuses/user_timeline)
$query = 'count=5&include_entities=true&include_rts=true&screen_name=micc1983';
var_dump( $twitter_api->query( $query ) );
© www.soinside.com 2019 - 2024. All rights reserved.