【问题标题】:Recent tweets using wp-twitter-api class最近使用 wp-twitter-api 类的推文
【发布时间】:2014-09-15 16:13:10
【问题描述】:

我尝试显示我最近的推文,

我使用Twitter API 1.1 Client for Wordpress

查看Twitter Result

显示我的名字有效,

但显示我最近的推文不起作用!

我想要解决方案,这是我的代码:

<?php 

require_once (TEMPLATEPATH . '/include/twitter-api/twitter-api.php'); 

$credentials = array(
'consumer_key'    =>  'i added my consumer_key',
'consumer_secret' =>  'i added my consumer_secret'
);

$twitter_api = new Wp_Twitter_Api( $credentials );

$query = 'screen_name=twitter_api&count=1';
$args = array(
    'type' => 'statuses/user_timeline',
    'cache' => ( 24 * 60 * 60 )
);
$result = $twitter_api->query( $query, $args );
echo $result->name; // it's working and I got my name
echo '<br>';
echo $result->text; // it's not working and I don't got my recent tweets, I want solution!
?>

【问题讨论】:

    标签: php wordpress api twitter


    【解决方案1】:

    我想这里的问题不止一个。

    1) 'type' 设置为 'statuses/user_timeline' $result->name 不应该返回任何值,因为您可以使用 twitter api 控制台 (http://bit.ly/twapiconsole) 进行验证,'statuses/user_timeline' 将返回一个推文数组。

    2) 我不确定您是否熟悉 PHP,但您无法回显数据数组,但您必须使用 foreach 循环遍历数组:

    // Your code here
    // ...
    
    $result = $twitter_api->query( $your_query, $your_args );
    foreach ( $result as $tweet ){
        echo $tweet->text;
    }
    

    3) 当您对如何处理 API 请求响应有疑问时,只需执行 var_dump($result)

    希望对你有所帮助。

    【讨论】:

    • 谢谢 MiCc83 先生,我试试看。
    • 我设置了 count=3,但我只收到了 1 条推文。
    • 使用bit.ly/twapiconsole,您通过 statuses/user_timeline 获得了多少条推文?你能粘贴完整的代码吗(当然没有 $credentials)?
    猜你喜欢
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2016-08-28
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 2013-12-25
    相关资源
    最近更新 更多