1

I am new to Symfony3 and trying to learn api connections.

Just using endroid_twitter bundle.

I copied twitter api keys to parameters.yml but when i try to use this keys on DefaultController.php its going wrong and can not get keys.

here: "undefined variables"

$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');

    }
}
onurkaya
  • 157
  • 2
  • 16
  • Scroll down to the end of instructions to see how to use this in a Symfony app. The basic approach is a bit different: https://packagist.org/packages/endroid/twitter – Cerad Jul 13 '17 at 10:51

1 Answers1

0

You need to make a different declaration to create a new twitter service.

Try this:

$twitter = $this->get('endroid.twitter');

instead of this:

$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171