Category Archives: PHP

Getting a twitter user’s latest posts using API v1.1 and Guzzle (PHP)

I’ll keep this brief. Install Guzzle however you wish, but I┬árecommend using┬ácomposer:

{
    "require": {
        "guzzle/guzzle": "~3.1.1"
    }
}

Then use the following code:

            $twitter_client = new \Guzzle\Http\Client('https://api.twitter.com/{version}', array(
                'version' => '1.1'
            ));
            $twitter_client->addSubscriber(new \Guzzle\Plugin\Oauth\OauthPlugin(array(
                'consumer_key'  => TWITTER_CONSUMER_KEY,
                'consumer_secret' => TWITTER_CONSUMER_SECRET,
                'token'       => TWITTER_ACCESS_TOKEN,
                'token_secret'  => TWITTER_ACCESS_TOKEN_SECRET
            )));
 
            $request = $twitter_client->get('statuses/user_timeline.json');
            $request->getQuery()->set('count', 5);
            $request->getQuery()->set('screen_name', 'YOURUSERNAME');
            $response = $request->send();
 
            $tweets = json_decode($response->getBody());

Don’t forget to put your constants somewhere.

Ensuring facebook canvas applications work in IE8 / IE9 / IE10

Last week I deployed a new Facebook application into a production environment. Everything seemed to be working perfectly, but then I received a report that the application wasn’t working correctly in IE8+. I narrowed down the problem, and then realised that sessions weren’t working. It turns out IE8+ has a security policy that prevents iframes from setting cookies if the parent domain is different. Therefore because my PHP session cooking wouldn’t set, the sessions obviously didn’t work between pages.

The good news is the fix is simple. Add this header to your page:

 header('p3p: CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"');

If you want to read more about this check this page:

http://en.wikipedia.org/wiki/P3P

Bonus: if using silex add this middleware:

$app->after(function (Request $request, Response $response) {
    $response->headers->set('p3p', 'CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"');
});

Theming/styling error messages in Symfony 2

I spent a large portion of my day today trying to customize the HTML produced by Symfony 2 for form errors. The documentation has a section on how to do this, but for the life of me, I could not make it work. Here is a working, re-usable solution, with a brief explanation of the mistake I made at the end.

In your SF2 bundle create a new file /Resources/views/Form/field_errors.html.twig:

{# YourBundleName/Resources/views/Form/field_errors.html.twig #}
{% block field_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<ul class="error_list">
    {% for error in errors %}
    <li>{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li>
    {% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endblock field_errors %}

Then import this into your TWIG template like so:

{% form_theme form '<YourNamespace><BundleName>:Form:field_errors.html.twig' %}

One key point to note here is that you must concatenate your name space and bundle name. So Namespace:BundleName won’t work but NamespaceBundleName will. This is the mistake I was making. It turns out that when you create a bundle it’s given an alias and by default it’s a concatenation of your namespace and bundlename, so when referencing it in TWIG it appears you need to use this alias.

Unique filenames when uploading using Zend_Filter_File_Rename (Zend Framework)

I came across a scenario today where I needed to ensure that files uploaded always had a unique name and wouldn’t over-write a file which has the same name. ZF provides this facility but it’s not particularly well documented and I had some troubles using a couple of examples I found across the web. Alas, here is the solution.
Continue reading

Bad character encoding causing MySQL errors in PHP

Today at work I encountered a peculiar problem when working with some translations. I developed a system whereby we can send out translations to our translators and they’ll fill in the blanks and send it back to use which is then imported. Part of this project involved coding a parser for a custom file format to send out to our translators. The benefit of this is it allowed me to validate incoming translation files, as well as easily export them according to the spec I’d defined.
Continue reading