Details make the difference

While playing around with the Zemanta API today, I bumped into a small problem. I first attempted to do it in symfony using the sfWebBrowserPlugin, but as I kept running into a 403 Developer Inactive error, I decided to try other tools, to see if the problem was on my side or on Zemanta's side. The problem, as it turned out, was on my side.

The Zemanta API is quite a simple API at this point, but it works nicely. To give some background, you feed the API with a text, Zemanta analyses this text, and offers links, images and tags as a suggestion for usage with the text. Excellent for weblogs and magazines looking to enrich their content. The API call therefore is nothing more than a POST request that requires a few parameters: text, return format, method (currently simply zemanta.suggest) and an API key. Should not be too hard, you'd think?

Think again
Since it's a simple request, I immediately decided to use the already installed sfWebBrowserPlugin for this request. It has a very simple API, and should work fine. However, I couldn't get it to work correctly. I kept getting a 403 Developer Inactive error. A weird error, which led me to believe I might be using an incorrect API key. I double checked it with the key in my control panel, but nope, everything was correct.

I need a second opinion
I wasn't making any progress, so on to the next attempt: Use another tool to do the same thing. I decided to dust off my Zend_Http_Client knowledge and use that instead. After 10 minutes of work (well, mostly reading into the exact syntax of doing the call) I had a working example. Including a valid response. So no problem on the Zemanta side, the developer was active! It was something in symfony or the plugin.

Let's squash that bug
I couldn't have it that something worked fine in Zend Framework, but wouldn't work in my beloved symfony. So I started trying other things, trying to chase the parameters sent with the request, but all seemed fine. After looks of searching through the code, trying the different adapters that come with sfWebBrowserPlugin, and comparing the approach of sfWebBrowserPlugin with the approach of Zend_Http_Client, I noticed one thing: the call to http_build_query() was different.

sfCurlAdapter was calling http_build_query($parameters) where Zend_Http_Client was calling http_build_query($this->paramsPost, '', '&' ). Notice the two extra parameters, of which the second is the most important in this situation. It turns out if you leave the seperator parameter empty, it defaults to the encoded &, which clearly the Zemanta API at least could not handle (and perhaps others as well). When I changed the call to http_build_query($parameters, null, '&' ), the problem was solved! I got back the expected XML response using the sfWebBrowserPlugin.

Contribute!
As I'd suggest every Open Source user to do, I immediately opened a bug for this so that it can be fixed by the developer of the plugin. Contributing back fixes like these to the project helps all users, and eventually makes the project so much better

Update!
It seems I have been stupid all along. The bug was already fixed in the sfWebBrowserPlugin a long time ago. I installed the plugin a long time ago into my project and never since updated it to the latest version, not even when encountering this bug. My bad, sorry Francois! :)


Add comment

Comments

gravatar Shahar Evron: I'm glad Zend_Http_Client could help ;)
July 9, 2008
gravatar Open: Zend_Http_Client works great!
July 18, 2008

Php5_zce_logo

not tested in IE


Upcoming events

I will be speaking 04-09-2009: Symfony Day 2009

Tags

1337 2008 4developers accessibility AdaLovelaceDay09 agavi agile amsterdam apache apple article articles atk atkMetaNode audioscrobbler backwards compatibility bbc beatstad best practices bittorrent book books bughuntday caching cake cal evans cat cerf certificate cfp clear cms community conference conferences continuous integration crisis css custom DbFinderPlugin decorator decorators deployment devdays development directoryindex documentation download dpc dpc09 DPC2008 dreamhost eclipse ed efficiency enterprise event events expertise flickr frameworks freeze frontend fun games germany getting real google googletalk graceful degradation hack hackers howto html http ibuildings icann ide imovie indy internet IPC ipc ipc08 javascript jobeet john peel joomla left on the web lime linux live london loudblog m2ts mac malware mambo marjolein meme meta methodology microsoft movie music mysql namespace namespaces nllgg odmarco open source ORM osx paradiso pear personal pfcongrez photo php phpabstract phpBB phpbb phpbelgium phpbenelux phpgg phpitalia phpnw phpnw08 phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations public qa recruiting refactoring review rewrite ruby on rails schedule script security seven things simplexml slides software sogeti solar standard standards static steer subversion symfony symfonycamp symfonyUnderControlPlugin talk technology techportal tek09 terratec terrorism testfest testing textpattern tips tld tomas usability usergroup vhost video vinyl virus warp weblogging wiki women world world of warcraft writing xml xpath yara year youtube ZCE zemanta zend zend framework zend server zend studio Zend_Form
© 2004 - 2009 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Delicious, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.4.0