Archive for April, 2012

It’s simple math

Gardening and edible landscaping serves up some incredible meals.

dinner-montage

edible landscaping gardening local seafood

How to send asynchronous requests in PHP

It’s easy to make asynchronous calls in PHP with just a little bit of HTTP header knowledge and some library code around PHP sockets.

This technique is useful when posting requests to your own server for bits of logic you’d like to run in the background.  If you don’t control the endpoint, you might not be comfortable with some of the limitations, such as the inability to read anything from the response.  So, you couldn’t post data to a webservice and receive an HTTP 200 OK response and certainly not an ID for an object newly created by the service call. Any bad IP address would give you an error and you’d also get an error if your socket couldn’t connect. This level of error handling might be sufficient for what you need.

For best performance, use IP addresses instead of DNS entries to prevent the need for look-up and resolution.

For fire and forget stuff, this is the bees knees:

(Please ignore my class definition and Controller superclass. This is actual working code from my CodeIgniter application where I scratched this out as a proof of concept)

class Scratch extends SQ_Controller {

    function Scratch() {
        parent::SQ_Controller();
    }

    function index() {

        echo "PHP Async Test...
";

        $params = array(
            "one" => "111111",
            "two" => "22222",
            "three" => "33333",
            "four" => "44444",
        );
        $this->curl_post_async("http://127.0.0.1/sq/scratch/longone", $params);
    }

    function longone(){

        $one = $_POST["one"];
        $two = $_POST["two"];
        $three = $_POST["three"];
        $four = $_POST["four"];

        echo uniqid("You won't see this because your PHP script isn't waiting to read any response");

        // put some long delay in here, so you can see how quickly the async requests returns
        sleep(5);

        // and the proof that something actually happens...  write out the HTTP params that were sent over the wire
        $fp = fopen('/PATH/TO/YOUR/DIR/FOR/OUTPUT/data.txt', 'w');
        fwrite($fp, $one);
        fwrite($fp, $two);
        fwrite($fp, $three);
        fwrite($fp, $four);
        fclose($fp);

    }

    function curl_post_async($url, $params = array()){

        $post_params = array();

        foreach ($params as $key => &$val) {
              if (is_array($val)) $val = implode(',', $val);
                $post_params[] = $key.'='.urlencode($val);
            }
            $post_string = implode('&', $post_params);

            $parts=parse_url($url);

            $fp = fsockopen($parts['host'],
                isset($parts['port'])?$parts['port']:80,
                $errno, $errstr, 30);

            $out = "POST ".$parts['path']." HTTP/1.1\r\n";
            $out.= "Host: ".$parts['host']."\r\n";
            $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
            $out.= "Content-Length: ".strlen($post_string)."\r\n";
            $out.= "Connection: Close\r\n\r\n";
            if (isset($post_string)) $out.= $post_string;

            fwrite($fp, $out);
            fclose($fp);
    }
}

A few rules of business and travel

Business Rule #17432: When travelling on business, work!  There are no interruptions.  There are no kids demanding attention.  It is quiet and it’s the perfect time to focus.  There is no one to answer to about anything, so open the laptop, turn off the TV (why it is even on?), and get to work.

Business Rule #9813: Unless your job requires you to schmooze clients and ply them with liquor, don’t drink.  Not only does it impede Business Rule #17432 above, but the last thing you need is to be hungover during the big conference the next day or not as quick as you’d like to be.  Get to bed early and hit a home run while you’re away.  You’re away from your family, you are sacrificing something, so make it worthwhile!

And this is just a suggestion, but try to get the elite hotel room: 1337 (for hackers only)

leet

Never let a consultant handle your core business

From I, Cringley

:

In Minneapolis, Best Buy is known as a body shop.  While they have something like 1000 IT workers, most of them are temporary contractors.  They come and go.  The number of IT people who are Best Buy employees is very small.  Too few to effectively set direction or do things well.  Best Buy depends on an army of consulting firms to do its IT planning and projects.  They do what they are asked to.  Unfortunately there is a big disconnect between what the business really needs and what IT is doing.

Never let a consultant handle your core business.  Every retailer needs to be in the IT business.  Walmart pioneered IT for retailers, what, 40 years ago?  All Best Buy had to do is follow.

Switch to our mobile site