【问题标题】:retrieving tweets using Abraham/twitteroAuth cakephp 3使用 Abraham/twitteroAuth cakephp 3 检索推文
【发布时间】:2015-11-16 13:18:28
【问题描述】:

我正在尝试使用 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
    }
]
}

【问题讨论】:

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


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 2013-08-15
      • 2012-11-28
      • 2015-09-25
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多