我是Symfony3的新手并且正在尝试学习api连接。
只需使用endroid_twitter捆绑包。
我将twitter api密钥复制到了parameters.yml,但是当我尝试在DefaultController.php上使用这个键时,它出错并且无法获取密钥。
此处:"未定义的变量"
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
parameters.yml
parameters:
database_host: 127.0.0.1
database_port: null
database_name: api
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: ThisTokenIsNotSoSecretChangeIt
endroid_twitter:
consumer_key: 'consumer_key'
consumer_secret: 'consumer_secret'
access_token: 'access_token'
access_token_secret: 'access_token_secret'
DefaultController.php
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Endroid\Twitter\Twitter;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$response = $twitter->query('/statuses/user_timeline', 'GET', 'json', $parameters);
$tweets = json_decode($response->getContent());
$text = $tweets[5]->text;
print_r($text);
return $this->render('default/index.html.twig');
}
}
答案 0 :(得分:0)
您需要制作不同的声明来创建新的Twitter服务。
试试这个:
$twitter = $this->get('endroid.twitter');
而不是:
$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);