我正在尝试使用Abraham/twitteroAuth: https://github.com/abraham/twitteroauth检索tweet。这是我的控制器:
<?php
namespace AppController;
use AppControllerAppController;
use AbrahamTwitterOAuthTwitterOAuth;
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);
}}
这是我的viewindex。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>
但是我没有收到任何tweet。当我尝试调试($tweets);在我的控制器中,我得到这个:
object(stdClass) {
errors => [
(int) 0 => object(stdClass) {
message => 'Sorry, that page does not exist'
code => (int) 34
}
]
}
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