Implementing Zend Framework 2 twitter service

Zend framework 2

Recently I got task to implement Twitter feed into application built on Zend Framework 2. I found that Zend Framework 2 is having ZendService\Twitter\Twitter library which provides a client for the Twitter API so I wanted to give it advantage over other libraries available on Packagist. Using this service it’s possible to get and update your status, reply to friends, direct message friends, block and ublock users, follow/unfollow people, mark tweets as favorites, and much more. I thought It would be easy and it actually was except one tiny quirk in official documentation of package.

First of all we have to add dependency into composer.json file

"zendframework/zendservice-twitter": "2.1.*",

and update only this package

php composer.phar update zendframework/zendservice-twitter

Our application can now use this package to use Twitter API. You have to get four keys from twitter to use it’s API :

  1. Access token – (array key value – ‘twitter-access-token-here’)
  2. Access token secret – (array key value – ‘twitter-access-secret-here’)
  3. API key – (array key value – ‘twitter-consumer-key-here’)
  4. API secret – (array key value – ‘twitter-consumer-secret-here’)

You will get this key after creating new app on this link. When you finish with this you can use this API in you Zend 2 application.

As official documentation says you have to define config varible like this

$config = array(
    'access_token' => array(
        'token'  => 'twitter-access-token-here',
        'secret' => 'twitter-access-secret-here',
    ),
    'oauth_options' => array(
        'consumerKey' => 'twitter-consumer-key-here',
        'consumerSecret' => 'twitter-consumer-secret-here',
    ),
    'http_client_options' => array(
        'adapter' => 'Zend\Http\Client\Adapter\Curl',
        'curloptions' => array(
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_SSL_VERIFYPEER => false,
        ),
    ),
);

and now let’s create an instance of twitter API client like this

$twitter = new Twitter($config);

If you set up your twitter client like this you should be ready to use it. I say should because actually this instance doesn’t have what it needs to connect to Twitter API. Lets check it – we can try to verify our credentials.

$twitter->accountVerifyCredentials();

and you will get following error

Twitter zf2 client error

If you take a look at the code that throws this error you will see that variable that should be valid adapter for http request is not defined. It should be defined in ‘http_client_options’ but this adapter is never instantiated. That means that we have to do this manually. We could do this on two ways. We can create and provide Http client class to the constructor of twitter api class or we can use default one, created by our client class and to add missing adapter to Http client instance. Lets do it

$config = array(
    'access_token' => array(
        'token'  => 'twitter-access-token-here',
        'secret' => 'twitter-access-secret-here',
    ),
    'oauth_options' => array(
        'consumerKey' => 'twitter-consumer-key-here',
        'consumerSecret' => 'twitter-consumer-secret-here',
    ),
);

$twitter = new Twitter($config);

$curlClient = new \Zend\Http\Client\Adapter\Curl();
$curlClient->setOptions(array(
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
    )
);
$httpClient = $twitter->getHttpClient();
$httpClient->setAdapter($curlClient);
$twitter->setHttpClient($httpClient);

$twitter->accountVerifyCredentials();

Thats it – you can use this service now.

Leave a Reply

Your email address will not be published. Required fields are marked *