Last active
April 14, 2021 12:24
-
-
Save YasuhiroYoshida/3126571 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Acme/TweetBundle/Controller/DefaultController.php | |
namespace Acme\TweetBundle\Controller; | |
use Symfony\Component\ClassLoader\UniversalClassLoader; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Themattharris\TmhOAuth; | |
use Acme\TweetBundle\Entity\SearchWord; | |
use Acme\TweetBundle\Entity\Makeover; | |
use Acme\TweetBundle\Form\Type\TweetSearchType; | |
use Symfony\Component\HttpFoundation\Request; | |
class DefaultController extends Controller | |
{ | |
public function __construct() | |
{ | |
// Obtain access to an OAuth 1.0A library written by @themattharris | |
// require_once "Themattharris/TmhOAuth.php"; | |
} | |
public function indexAction(Request $request) | |
{ | |
// Create an entity that is going to take care of a search word | |
$searchWord = new SearchWord(); | |
$searchWord->setSearchWord('Enter a search word'); | |
// Create a form object | |
$form = $this->createForm(new TweetSearchType(), $searchWord); | |
// This will eventually refers to fetched tweets | |
$tweets = array(); | |
// When a search word is submitted, it's a POST method that is used | |
// And when proven valid, it will trigger communication with Twitter Search API | |
if ($request->getMethod() == 'POST') { | |
$form->bindRequest($request); | |
if ($form->isValid()) { | |
// Obtain access to an OAuth 1.0A library written by @themattharris | |
// Send a query upon successful authentication | |
$twitter = new TmhOAuth(array("host" => "search.twitter.com")); | |
$twitter->request("GET", $twitter->url("search"), | |
array("q" => $form->get('search_word')->getData(),)); | |
// Receive + decode results from Twitter | |
$results = json_decode($twitter->response["response"])->{"results"}; | |
// Have the results processed so that the representation will be more suitable for the view | |
$makeover = new Makeover(); | |
$tweets = $makeover->stackTweets($results); | |
} | |
} | |
// Give the representation to the View | |
return $this->render( | |
'AcmeTweetBundle:Default:index.html.twig', | |
array('form' => $form->createView(), 'tweets' => $tweets,)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I completely forgot why I put this code here. Possibly, the repo was private back then, but I was asked to show part of it to somebody outside...🤔