Skip to content

Instantly share code, notes, and snippets.

@mcuadros
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcuadros/9604673 to your computer and use it in GitHub Desktop.
Save mcuadros/9604673 to your computer and use it in GitHub Desktop.
stream_get_meta_data behavior

fopen

Code

<?php
$fp = fsockopen("www.example.com", 80);
if (!$fp) {
    echo "Unable to open\n";
    exit();
}
 
fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
stream_set_timeout($fp, 2);
$res = fread($fp, 2000);
 
$info = stream_get_meta_data($fp);
var_dump($info);

Output PHP

array(7) {
  'stream_type' =>
  string(14) "tcp_socket/ssl"
  'mode' =>
  string(2) "r+"
  'unread_bytes' =>
  int(0)
  'seekable' =>
  bool(false)
  'timed_out' =>
  bool(false)
  'blocked' =>
  bool(true)
  'eof' =>
  bool(false)
}

Output HHVM

array(10) {
  ["wrapper_type"]=>
  string(4) "File"
  ["stream_type"]=>
  string(0) ""
  ["mode"]=>
  string(0) ""
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(false)
  ["uri"]=>
  string(0) ""
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(false)
  ["eof"]=>
  bool(true)
  ["wrapper_data"]=>
  NULL
}

socket

Code

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$dest = gethostbyname("www.example.com");
socket_connect($socket, $dest, 80);
socket_write($socket, "GET / HTTP/1.0\r\n\r\n");
stream_set_timeout($socket, 0, 1);
 
$res = '';
socket_recv($socket, $res, 2000, MSG_WAITALL);
$info = stream_get_meta_data($socket);
var_dump($info);
var_dump($res);

Output PHP

Warning: stream_set_timeout(): supplied resource is not a valid stream resource in /private/tmp/test.php on line 7
 
Call Stack:
    0.0002     232712   1. {main}() /private/tmp/test.php:0
    0.1595     233368   2. stream_set_timeout() /private/tmp/test.php:7
 
 
Warning: stream_get_meta_data(): supplied resource is not a valid stream resource in /private/tmp/test.php on line 11
 
Call Stack:
    0.0002     232712   1. {main}() /private/tmp/test.php:0
    0.3116     235688   2. stream_get_meta_data() /private/tmp/test.php:11
 
bool(false)
string(497) "......"
```
 
### Output HHVM

array(10) { ["wrapper_type"]=> string(4) "File" ["stream_type"]=> string(0) "" ["mode"]=> string(0) "" ["unread_bytes"]=> int(0) ["seekable"]=> bool(false) ["uri"]=> string(0) "" ["timed_out"]=> bool(false) ["blocked"]=> bool(false) ["eof"]=> bool(false) ["wrapper_data"]=> NULL } NULL


versions
--------
PHP 5.5.10 (cli) (built: Mar 16 2014 19:21:43)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
    
HipHop VM 2.5.0-dev+2014.03.17 (rel)
Compiler: heads/master-0-g2bd460c8de9012047f95b664670bc9c8127905f2
Repo schema: 72e4c298b836be72c3ea8a93bdcaffc06a2ccb04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment