PHP Socket context options


Introduction

Access to filesystem and various other stream wrappers can be customized by various context options and parameters configures by stream_context_create() and stream_context_set_option() functions.

Following list shows various socket context options are available for all wrappers that work over sockets, like tcp, http and ftp.

bindtospecifies the IP address (either IPv4 or IPv6) and/or the port number used to access the network. (ip:port for IPv4 [ip]:port for IPv6).
backloglimits number of outstanding connections in socket's listen queue.
ipv6_v6onlyOverrides the OS default regarding mapping IPv4 into IPv6.
so_reuseportAllows multiple bindings to a same ip:port pair.
so_broadcastEnables sending and receiving data to/from broadcast addresses.
tcp_nodelayIf TRUE, sets SOL_TCP,NO_DELAY=1 appropriately, disabling TCP Nagle algorithm.

Example

<?php
// connect to the internet using the '192.168.0.100' IP
$opts = array(
   'socket' => array(
      'bindto' => '192.168.0.100:0',
   ),
);
// ...and use it to fetch the data
echo file_get_contents('http://www.example.com', false, $context);
?>

Updated on: 21-Sep-2020

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements