使用Abraham / twitteroAuth cakephp 3检索推文

时间:2015-08-22 19:20:55

标签: php twitter twitter-oauth cakephp-3.0 tweets

我试图使用Abraham / twitteroAuth检索推文:https://github.com/abraham/twitteroauth。这是我的控制器:

<?php
namespace App\Controller;
use App\Controller\AppController;
use Abraham\TwitterOAuth\TwitterOAuth;
class TweetsController extends AppController
{
public function index()
{
  $oauth_access_token = 'XXXXXXXX';
  $oauth_access_token_secret = 'XXXXXXXX';
  $consumer_key = 'XXXXXXXXXX';
  $consumer_secret = 'XXXXXXXX';
  $connection = new TwitterOAuth($consumer_key,$consumer_secret,$oauth_access_token,$oauth_access_token_secret);
  $tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=hello&count=5");
  $this->set('tweets', $tweets);
}}

这是我的观点index.ctp:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Twitter search</title>
</head>
<body>
<div class="tweets index large-10 medium-9 columns">
    <?php foreach ($tweets as $tweet): ?>
            <?php foreach ($tweet as $t): ?>
                <p><?= $t->text ?><br></p>; 
            <?php endforeach; ?>
    <?php endforeach; ?>
</div>
</body>
</html>

但我没有收到任何推文。当我尝试调试($ tweets);在我的控制器中,我得到了这个:

object(stdClass) {
errors => [
    (int) 0 => object(stdClass) {
        message => 'Sorry, that page does not exist'
        code => (int) 34
    }
]
}

1 个答案:

答案 0 :(得分:0)

TwitterOAuth :: get()只需要路径,而不是完整的url,参数需要作为数组给出,而不是查询字符串。试试这个:

$tweets = $connection->get("search/tweets.json", ['q' => 'hello', 'count' => 5);

查看TwitterOAuth的源代码以了解更多信息:https://github.com/abraham/twitteroauth/blob/master/src/TwitterOAuth.php#L177