SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,023
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)

    PHP Function of the Week: stream_get_transports

    Array of PHP functions, shuffle and pop -
    Tell us the which function we'll learn this stop...


    stream_get_transports


    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?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    +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"

  3. #3
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    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!"

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    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).
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by cpradio
    ... your big/little endians
    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.

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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:
    PHP Code:
    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...
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    function compress($jsContent)
    {
        
    // Open process
        
    $cmd 'java -jar compiler.jar';
        
    $descriptors = array(
            
    => array('pipe''r'),
            
    => array('pipe''w'),
            
    => 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;

    "First make it work. Then make it better."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •