2013-08-21

Twitter from the coolest Data Warehouse

Last week a colleague said - Why don’t we Twitter the status from our Data Warehouse servers?

My reply was I tried to set it up a year ago or so but I failed. I used the Zend Framework one dot something. But of course I had to try again. So I started by downloading the Zend Framework 2 version 2.2.2. According to the documentation this was a simple task. After failing a number of times I realised the Twitter class was not in the the vs 2.2.2, after some googling around I found the code and installed it. But still my code did not find the ZF2 twitter code. I could not get the ZF2 autoloader to work so I had to require the ZF2 twitter code manually. Now there were some Oauth classes missing, after some googling I found the code and installed it. I could not get the autoloader to include the Oauth code so I had to require the code manually. And now I descended  into class bonanza hell, the Twitter and Oauth classes referred to other classes and interfaces and abstractions and God knows what. Since the autoloader is too hard for me I had to load all manually, trial and error until I had included all code needed to set up my classes to call Twitter. I was able to verify my credentials and fetch basic information about my Twitter account. But when I tried to post ‘Hello World!’ I got error Http return code 400. After trying a good number of times and googling a lot without any result, I had to give up.

One aspect I do not like with Object Orientation is the extreme complexity. All OO projects starts with a few simple classes with logical methods. But as the system grows (and changes) new classes are introduced which inherits from the original simple classes with almost identical methods. And then hell break loose, now come interfaces, roles, traits, mixins, abstractions, adapters and God knows what. And all these entities should be coupled and attached to each other in ways only logical to the developer. It is no simple task to dig into lots of alien errors like:  

PHP Fatal error:  Uncaught exception 'Zend\Http\Client\Adapter\Exception\RuntimeException' with message 'Error in cURL

request: Received HTTP code 400 from proxy after CONNECT' in /home/tooljn/PHPZF/ZendFramework-2.2.2/library/Zend/Http/Client/Adapter/Curl.php:422

Even if you have stack traces (which always are very long) you wonder what code is executed where and why?

There is seldom intuitivity in OO system, i.e. easy to understand and learn how connect OO object, you very often have to learn case by case.

This is more or less how my ZF2 code looked like when I gave up:

require_once 'Zend/Loader/StandardAutoloader.php';

$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));

// Register with spl_autoload:

$loader->register();

require_once 'Zend/ZendService/Twitter/Twitter.php';

require_once 'Zend/ZendService/Twitter/Response.php';

require_once 'Zend/ZendOAuth/OAuth.php';

require_once 'Zend/ZendOAuth/Config/ConfigInterface.php';

require_once 'Zend/ZendOAuth/Config/StandardConfig.php';

require_once 'Zend/ZendOAuth/Client.php';

require_once 'Zend/ZendOAuth/Http/Utility.php';

require_once 'Zend/ZendOAuth/Token/TokenInterface.php';

require_once 'Zend/ZendOAuth/Token/AbstractToken.php';

require_once 'Zend/ZendOAuth/Token/Access.php';

require_once 'Zend/ZendOAuth/Signature/SignatureInterface.php';

require_once 'Zend/ZendOAuth/Signature/AbstractSignature.php';

require_once 'Zend/ZendOAuth/Signature/Hmac.php';

array_key_exists('host',$job) ? $hostInfo = $xmlconverter['host'][0]['value'] : $hostInfo = 'twitter';

array_key_exists('app',$job) ? $tapp = $xmlconverter['app'][0]['value'] : $tapp = 'DW status';

$txml = getHostSysDsn($context, $hostInfo, "$tapp");

if (!$txml) return FALSE;

$curl_config = array(

        'adapter' => 'Zend\Http\Client\Adapter\Curl',

        'curloptions' => array(

            CURLOPT_SSL_VERIFYHOST => false,

            CURLOPT_SSL_VERIFYPEER => false));

           

$twitter_config = array(

    'access_token' => array(

        'token'  => $txml['Accesstoken'],

        'secret' => $txml['Accesstokensecret'],

    ),

    'oauth_options' => array(

        'consumerKey' => $txml['Consumerkey'],

        'consumerSecret' => $txml['Consumersecret'],

    ),

    'http_client_options' =>$curl_config

);

//var_dump($twitter_config);

$twitter = new ZendService\Twitter\Twitter($twitter_config);

$twitter->statuses->update('Hello World!');

The last line blows up with an error code 400 from Twitter, I have no idea why.

(In all fairness I should link to a post I wrote a long time ago with a cool example using  Zend Framwork1 at the end  .)

I do not think ZF2 is bad , it’s just too complex for me, and this often the case with OO systems. OO invites you to go complex over time. But this does not have to be, in this particular case I found an OO gem , A very simple class for posting on Twitter (the author  David Grudl  claims you can do any Twitter task from this class). Using David’s OO class I only had to write this code:

$message = trim(substr($job['message'][0]['value'],0,140));

$log->logit('Info',"Enter script=$scriptName message=$message");

require_once '/home/tooljn/TWITTER/twitter-php-master/src/twitter.class.php';

array_key_exists('host',$job) ? $hostInfo = $xmlconverter['host'][0]['value'] : $hostInfo = 'twitter';

array_key_exists('app',$job) ? $tapp = $xmlconverter['app'][0]['value'] : $tapp = 'DW status';

$txml = getHostSysDsn($context, $hostInfo, "$tapp");

if (!$txml) return FALSE;

$twitter = new Twitter($txml['Consumerkey'], $txml['Consumersecret'], $txml['Accesstoken'], $txml['Accesstokensecret']);

try {

        $tweet = $twitter->send("$message");

        $_RESULT = TRUE;

} catch (TwitterException $e) {

        $log->logit('Errror', $e->getMessage());

}

return $_RESULT;

And the code posted on Twitter without any fuzz on first attempt. The code was run from this job :

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>

<schedule mustcomplete='yes' logmsg='This is a Twitter test'>

   <job name='twittaMsg' type='script' pgm='twitter01.php'>

      <iconv input='UTF-8' output='UTF-8' internal='UTF-8'/>

      <message>@TODAY-@NOW Hello Twitter</message>

  </job>

</schedule>

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete