Browsing the archives for the Zend Framework category.

Zend Framework Twitter and oAuth in 1.10.7

Code, oauth, php, twitter, Zend Framework

Today I wanted need to switch out some implementations I have in twitter. I was using HTTP Basic for authentication as were most. I went through the Zend Oauth docs, and combined with their examples and those of Pádraic Brady I got this working fine.

I’m not going to give a step by step, as I’m assuming you’ve already read various docs.

I broke my files out like Pádraic, so if you’ve read his than you’ll understand mine.

First, my config file which is included within each subsequent file.

$config = array(
        'callbackUrl'    => '',
        'siteUrl'        => '',
        'consumerKey'    => 'XXXXXXXXXXXXXXXXXXXXX',

My index page is the starting point of the authentication process. Our persistent storage for this example is in session, though in production I’m using a database.

include_once './config.php';

    // $config is from our include!
    $consumer = new Zend_Oauth_Consumer($config);

    // fetch a request token
    $token = $consumer->getRequestToken();

    // persist the token to storage
    $_SESSION['TWITTER_REQUEST_TOKEN'] = serialize($token);

    // redirect the user

The callback url specified will now be triggered

include_once './config.php';

// Check if _GET is set and that the request token is being returned
if (!empty($_GET) && isset($_SESSION['TWITTER_REQUEST_TOKEN'])) {

    $consumer = new Zend_Oauth_Consumer($config);
    $token = $consumer->getAccessToken($_GET, unserialize($_SESSION['TWITTER_REQUEST_TOKEN']));

    $_SESSION['TWITTER_ACCESS_TOKEN'] = serialize($token);

    // Now that we have an Access Token, we can discard the Request Token
} else {
    // Mistaken request? Some malfeasant trying something?
    exit('Invalid callback request. Oops. Sorry.');

// Lets just pass on to a test posting page

Finally, if all went to plan and the user authorized our application, we can pull in their access token and do as we need on their behalf.

include_once './config.php';

// Verify we're good to go

    // Unserialize and add the token to the config
    $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
    $config['accessToken'] = $token;

    // Pass the config into Twitter, and act upon the users account
    $twitter = new Zend_Service_Twitter($config);

    $twitter->status->update('I am so posting from your account. Rock!');

That’s all really. I do wish the Zend Service Twitter docs would be updated to reflect some examples. I saw in SVN Pádraic has already changed some of the docs, they’ve just not been updated on site yet.

Thanks to the documentation authors and those I borrowed from to get my working example going and so I could more easily move forward with this project.


No Comments

Zend Soap Client timeout

php, Zend Framework

I’m working with a web service that’s showing to be slow at times. The default timeout in PHP is 30 seconds currently, so waiting around for it to timeout kinda sucks.

I decided to set the timeout to 10 seconds, to my surprise the timeout code is completely commented out in the class.

However they do provide a setStreamContext(), so I set http to timeout after 10 seconds like so:

$context = stream_context_create(array('http' => array('timeout' => 10)));

When the service slowed down to 15 second wait times, it died out early like it should have. I’m catching the exception, which isn’t pretty. Wish I knew when it was timing out, so I could log that specific case.

No Comments

Zend Form Element Radio and Default value

Code, Zend Framework

I setup a Zend Form object and needed radio buttons, but also needed a default value. Took me a few minutes to find, but here’s an example of use.

 * Setting some values for our radio buttons.
$options = array(
    '1' => 'Option 1',
    '2' => 'Option 2',
    '3' => 'Option 3'

 * The array('value' => 2) below sets our default value.
$radio = new Zend_Form_Element_Radio('elementName', array('value' => '2'));
No Comments