简体   繁体   English

使用Abraham / twitteroAuth cakephp 3检索推文

[英]retrieving tweets using Abraham/twitteroAuth cakephp 3

I'm trying to retrieve tweets using Abraham/twitteroAuth : https://github.com/abraham/twitteroauth . 我正在尝试使用Abraham / twitteroAuth检索推文: https : //github.com/abraham/twitteroauth this is my controller: 这是我的控制器:

<?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);
}}

And this is my view index.ctp: 这是我的视图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>

but i'm not getting any tweets. 但我没有收到任何推文。 when i try debug($tweets); 当我尝试调试($ tweets); in my controller i get this : 在我的控制器我得到这个:

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

TwitterOAuth::get() only needs the path, not the full url, and parameters need to be given as an array, not a query string. TwitterOAuth :: get()仅需要路径,而不是完整url,并且参数需要作为数组而不是查询字符串给出。 Try this instead: 尝试以下方法:

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

Review TwitterOAuth's source code to learn more: https://github.com/abraham/twitteroauth/blob/master/src/TwitterOAuth.php#L177 查看TwitterOAuth的源代码以了解更多信息: https : //github.com/abraham/twitteroauth/blob/master/src/TwitterOAuth.php#L177

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM