streams gem

Was reminded today of a gem from the streams extension - sending a POST message and getting its results. We could do it using cURL or sockets - but why work that hard. Being lazy does have its advantages after all.

Now I could make up an example for this - but why when the manual has such a nice one (though it is a bit buried in the manual on the HTTP/HTTPS wrapper page):

[php]
< ?php

$postdata = http_build_query(
array(
'var1' => ’some content’,
‘var2′ => ‘doh’
)
);

$opts = array(’http’ =>
array(
‘method’ => ‘POST’,
‘header’ => ‘Content-type: application/x-www-form-urlencoded’,
‘content’ => $postdata
)
);

$context = stream_context_create($opts);

$result = file_get_contents(’http://example.com/submit.php’, false, $context);

?>
[/php]

5 Responses to streams gem

  1. ekini web development blog :

    thanks for this. i am already thinking of using this… hehe being lazy has its advantages…

  2. Mike :

    He, nearly as simple as

    http_post_fields(”http://example.com/submit.php”, array(”var”=>”val”));

    ;P

  3. Paul Reinheimer :

    While I’ve often used streams, I’m also a fan of context for doing HTTP posts as well :)

  4. bob :

    bookmark you thx

  5. kris :

    nice work man 10x

Leave a Reply