GET search/tweets with ZendService\Twitter

1. Install ZendService\Twitter

comporser.json

"require": {
    "zendframework/zendservice-twitter": "2.1.*"
}

install

php composer.phar install or update

 2. Create an twitter application

Prepare below keys to use twitter api

API key, API secret, Access token, Access token secret 

 

3. ZendService\Twitter\Twitter\Twitter instance

in your contoroller

use ZendService\Twitter\Twitter;

class FooController extends AbstractActionController
{
    public function twitterSearchAction() {
        $apiKey = 'YOUR_API_KEY';
        $apiSecret = 'YOUR_API_SECRET';
        $accessToken = 'YOUR_ACCESS_TOKEN';
        $accessTokenSecret = 'YOUR_ACCESS_TOKEN_SECRET';
        
        $config = array(
            'access_token' => array(
                'token'  => $accessToken,
                'secret' => $accessTokenSecret,
                ),
            'oauth_options' => array(
                'consumerKey' => $apiKey,
                'consumerSecret' => $apiSecret,
                ),
            'http_client_options' => array(
                'adapter' => 'Zend\Http\Client\Adapter\Curl',
                'curloptions' => array(
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_SSL_VERIFYPEER => false,
                    ),
                ),
            );

        $twitter = new Twitter($config);
        
        // Verify your credentials:
        $response = $twitter->account->verifyCredentials();
        if (!$response->isSuccess()) {
           die('Something is wrong with my credentials!');
        }

        // Search for something:
        $response = $twitter->search->tweets('#zf2');
        foreach ($response->toValue()->statuses as $tweet) {
            printf("%s\n- (%s)\n", $tweet->text, $tweet->user->name);
        }
    }
}

ZendService\Twitter — Zend Framework 2 2.3.1 documentation - Zend Framework