- BASICS
- Quotes
- Constants
- Control Structures
- Reference
- Number Systems
- VARIABLES
- Definition
- Variable Variable
- Exists
- Type Casting
- OPERATORS
- Aritmetic
- Bitwise
- String
- Comparison
- Logical
- FUNCTION
- Definition
- Anonymous
- Reference
- Variable Arguments
- ARRAY
- Basics
- Operations
- Create
- Search
- Modify
- Sort
- Storage
- STRING
- String Basics
- String Compare
- String Search
- String Replace
- String Format
- String Regexp
- String Parse
- Formating
- Json
- STREAMS
- File Open
- Read File
- Read Csv
- File Contents
-
Context
- Ob_start
- OOP
- Object Instantiation
- Class Constructor
- Interfaces, Abstract
- Resource Visibility
- Class Constants
- Namespaces
- HTTP
- Headers
- File Uploads
- Cookies
- Sessions
Wrapper
Get the entire contents of the webpage as if it is a local file.
$contents = file_get_contents('http://studyon.minte9.com');
echo $contents;
Stream context
Allows you to add extra parameters to be used with streams.
$postVars = array(
'comment' => 'Test by stream',
'page_id' => '328',
'page_type' => '1',
);
//echo file_get_contents("http://studyon.minte9.com/login"); die;
$wrapperOptions = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($postVars, '', '&'),
'timeout' => 5,
)
);
$streamContext = stream_context_create($wrapperOptions);
echo file_get_contents("http://www.fanlafel.ro/add-comment", 0,
$streamContext);
// Insert comment
You can create socket servers and clients using the stream functions.
// server.php
$socket = stream_socket_server("tcp://0.0.0.0:1037");
while ($conn = stream_socket_accept($socket)){
fwrite($conn, "Hello World");
fclose($conn);
}
fclose($socket);
// client.php
$socket = stream_socket_client("tcp://0.0.0.0:1037");
if ($socket) {
while(!feof($socket)) {
echo fread($socket, 100);
}
fclose($socket);
}
Finally, we can run our server just like any other PHP script.
$ php ./server.php
$ php ./client.php
Hello World
You can add a filter to the beginning and end to a stream.
// server.php
$socket = stream_socket_server("tcp://0.0.0.0:1037");
while ($conn = stream_socket_accept($socket)){
// add filters
stream_filter_append($conn, '<span class='keyword_code'>string.toupper</span>');
stream_filter_append($conn, 'zlib.deflate');
fwrite($conn, "Hello World");
fclose($conn);
}
fclose($socket);
// client.php
$socket = stream_socket_client("tcp://0.0.0.0:1037");
// add filter
stream_filter_append($socket, 'zlib.inflate');
if ($socket) {
while(!feof($socket)) {
echo fread($socket, 100);
}
fclose($socket);
}
Last update: 531 days ago