Archive for the ‘PHP code’ Category

Raw query on Zend framework

\Zend_Db_Table_Abstract::getDefaultAdapter()->query($sql); \Zend_Db_Table_Abstract::getDefaultAdapter()->query($sql)->fetch(); //if you need to fetch results if it does not work, use the db handler Pdo), and call – for example – exec() \Zend_Db_Table_Abstract::getDefaultAdapter()->getConnection()->exec($sql);

PHP Command line interface, set and read ENV variables

When a variable on the console is set export VARIABLE=VALUE; You can get it from PHP Command line (CLI) using getenv(‘VARIABLE’)   Example #script.php <?php var_dump(getenv(‘VAR’)); ?> From command line php -f script.php //bool(false) export VAR=a; php -f script.php //string(1) “a”  

Useful options for PHP Command line interface

I find PHP command line interface very useful, for CRON jobs, testing and whenever apache is not necessary. Basic commands here: CLI php.net manual. In this article I’m writing some CL flags and few lines of related functions / PHP code I find useful To make script interactive and read the line from the console $var [...]

Automatic caching of Zend_Db (or any object) calls

I’m working on a personal Zend framework application that uses Zend_Db_Table_Abstract classes to get data from the database. Each class has its own methods (getall, getXX, getYYY) etc… My need was having all those method calls (around the code) automatically cached. The first solution in my mind was a general wrapper (container) class that uses [...]

Script to download Youtube videos and merge into an AVI file

I had the need to watch a TV program on youtube splitted into 12 videos when I was on holiday at home. Due to my poor connection there, I decided to obtain the merged version in AVI format and watch it on the TV screen via HD player. I did not found any software/script that [...]

PHP float bug with 2.2250738585072011e-308

I’ve recently been informed that a serious PHP bug has been discovered. Basically, when PHP convert the value 2.2250738585072011e-308 (any manipulation such as conversion to int or sum with another value) it hangs and stop working. See discussions and more info http://bugs.php.net/bug.php?id=53632 http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ http://news.ycombinator.com/item?id=2066084 Consequences are trivial to foresee: a site can be hacked by [...]

Post to wordpress using Zend_XmlRpc Client

WordPress supports metaWeblob API standard to post data into the blog from an external service. WordPress contains xmlrpc.php [source], that is basically a XML RPC server that can be called from a client that posts XML RPC messages. See here the wordpress codex guide. Sending XML message is a task very easy with zend framework [...]

SVN pre-commit hook script to check PHP syntax and CodeSniffer

SVN supports pre and post-commit hook scripts. The former can be used to do some checks on the code before having it committed. In the example below, a hook script I use (on a dreamhost server of mine) to check if the committed files contain PHP syntax errors or the code doesn’t satisfy the code [...]

Using PHP Closures to get cached object with one call

Getting caching objects if a fairly frequent task when dealing with performative web applications. Standard approach if (cache object is valid)  { return cached object } else { get fresh object (*) save object into cache return object } I don’t like that approach as there are a lots of LOC that wraps the only [...]

The power of the Regular expression on the web

Regular Expression are Swiss-knife tools developers should know, extremely useful for  web-purposes and unavoidable when dealing with content scraping. A single call of a preg_match_all with a medium complex regular expression could save hundres of LOC and time as well as obtaining much more maintainable code. There  are advanced features like subpatterns and conditional expressions. They [...]