PHP Function of the Week: stream_get_transports

[CENTER]Array of PHP functions, shuffle and pop -
Tell us the which function we’ll learn this stop…

[fphp]stream_get_transports[/fphp][/CENTER]

Ok, I’m gonna be honest - in ten years of working with PHP, eight of which have been professionally, I’ve never called this function or any of its siblings. I started to do some research on this last week, but I got caught up on work. I know what streams are in principle - basically they are ways of linking up Unix programs to one another. But beyond that, I’m not sure.

So, who wants to step up to the plate and go over this one?

+1 with you on this. The only reason I know of it was for when swotting up for my ZCE, even then I don’t thing I grasped it at all.

Is there any example anyone can show us of this being used in anger? Else, could we contrive to use it for something … at all?

I have a particular challenge coming up where I want to get the diffs between 2 .txt files, there are PHP diff implementations around I know I could use - and have not had time to sit and assess them yet.

Is this something that streams could help me with?

If so I’ll post a fuller spec.

These more esoteric PHP functions remind me a bit of learning design patterns, at some stage you have to go looking for reasons to use them, even if they are used in the wrong place – its only after studying them and maybe discovering more about what they cannot do rather than what they actually do, it is only then that one groks it to its fullness – or as we are more likely to say, “Ah, **** yeah, NOW I get it – that’s clever”

Ooh interesting! I like this idea for topics! Have you done any others? (I’m not as active around here as I once was I’m afraid so I’ve missed them.) If so, it’d be good to have a list of these topics in a sticky or something.

Anyway, back on topic. I’m in the same boat! Been developing in PHP since back in the .php3 days and have yet to make use of any of the stream_* functions.

As you and Cups said, it’d be interesting to find at a piece of code using them and think “Ooh, that’s clever!”

I’m in the same situation as everyone else (that I’ve never actually used the stream* functions), but if I had to take a guess, I imagine these become useful when you want to communicate with other systems on the network over TCP, UDP, etc. Why? Because it is fast (as a comparison, it is said that using TCP/IP bindings in .NET Services is 1000 times faster than the HTTP bindings). Granted there are a lot of rules you usually have to abide by but if you develop the receiver and the sender, you can likely define your own rules.

Other things you may have to take notice on are your big/little endians since you are dealing with network data. It would also stand to reason you would be responsible for encrypting your data since you are going to the network transport directly (if you are concerned with someone sniffing your network and picking up your packets).

Another thing I only found out about for my ZCE exam, this one did stick in my head though because I found it so interesting/amazing/basic.

Github mentions of stream_get_contents, a cursory glance of the last one filicious on that list only mentions it in the readme – that I could see anyway.

That actually looks like an appropriate use, as it is retrieving, writing, or appending the file contents of a remote file via FTP transport. I was at a conference probably 2 years ago where the person was using TCP/IP communication to interact with a field device for scientific research.

In short, he was given an API detailing the stream headers, commands, etc that you can send over a specified port to the device and it would respond back with the information you asked for. I don’t quite recall the language he was using (I think it may have been .NET with a C++ satelite DLL for some of the transport routines), but it was interesting nonetheless.

The only problem is I don’t see PHP being the language I’d choose for these operations. Most of the time you want to do relays or verbose communication in these situations where you need threading or stacks of some sort. PHP just doesn’t seem well vetted for the scenario (but again, I always state, choose the right language for the job, don’t fit the job into a language).

Where PHP may be able to use this cleverly is back when IP cameras were not web accessible already (meaning they ran their own built in program) and you had to interact with them via TCP/IP to get the picture/footage and the communication would be fairly short, authenticate me, thanks, now give me the latest image! streams always gave me the feeling of having to be very much involved in the networking side of IT and I despise that side (I’m just not cut out for it!), so I tend to stay away from it.

What could be an interesting challenge, is to see which performs better:

file_get_contents('ftp://somelocation.com/pub/myfile.txt');

Or using stream_get_contents after verifying the tcp transport is available for use.

Hmm, I may have to add that to my to do list…

I know what streams are in principle - basically they are ways of linking up Unix programs to one another.

I think this statement better describes sockets that streams.

Streams are commonly used to read/write files, get user input from command line, etc.

http://www.php.net/manual/en/intro.stream.php

Streams were introduced with PHP 4.3.0 as a way of generalizing file, network, data compression, and other operations which share a common set of functions and uses. In its simplest definition, a stream is a resource object which exhibits streamable behavior. That is, it can be read from or written to in a linear fashion, and may be able to fseek() to an arbitrary locations within the stream.

Imagine writing to and reading from a file. Now imagine writing to and reading from some non-file resource in exactly the same way. Streams abstract away the details of what you’re operating on. It could be a file, a network, a command line program, or others. But as far as the stream functions are concerned, these are all the same thing – a writable, readable resource.

I used the stream functions once when I wrote code to minify on the fly. I used streams to write input to and read output from the Closure Compiler.

function compress($jsContent)
{
    // Open process
    $cmd = 'java -jar compiler.jar';
    $descriptors = array(
        0 => array('pipe', 'r'),
        1 => array('pipe', 'w'),
        2 => array('pipe', 'w')
    );
    $process = proc_open($cmd, $descriptors, $pipes);
    if ($process === false) {
        die('proc_open failed');
    }

    // Write raw
    fwrite($pipes[0], $jsContent);
    fclose($pipes[0]);

    // Read compressed
    $compressed = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // Check errors
    $errors = stream_get_contents($pipes[2]);
    fclose($pipes[2]);
    $exitStatus = proc_close($process);
    if ($errors != '' or $exitStatus != 0) {
        die('errors or exit status failed');
    }

    return $compressed;
}