note 76468 modified in function.fsockopen by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 19:28:17 +0000 |
| Subject: | note 76468 modified in function.fsockopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142295@lists.php.net to get a copy of this message | ||
I ran into a problem where I needed to make an fsockopen() style connection from one specific ip
address on a server with multiple ip aliases. Unfortunately fsockopen() does not allow the
specification of a source address making it unsuitable. I could not find any other solutions
elsewhere so here is mine for those with php 5.1.0 or greater.
<?php
// connect to the internet using the '192.168.0.100' IP
$opts = array('socket' => array('bindto' => '192.168.0.100:0'));
$context = stream_context_create($opts);
$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30,
STREAM_CLIENT_CONNECT, $context);
?>
Now $fp can be used like a resource handle returned by a vanilla fsockopen() call!
--was--
I ran into a problem where I needed to make an fsockopen() style connection from one specific ip
address on a server with multiple ip aliases. Unfortunately fsockopen() does not allow the
specification of a source address making it unsuitable. I could not find any other solutions
elsewhere so here is mine for those with php 5.1.0 or greater.
// connect to the internet using the '192.168.0.100' IP
$opts = array('socket' => array('bindto' => '192.168.0.100:0'));
$context = stream_context_create($opts);
$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30,
STREAM_CLIENT_CONNECT, $context);
Now $fp can be used like a resource handle returned by a vanilla fsockopen() call!
http://php.net/manual/en/function.fsockopen.php