note 76468 deleted from function.fsockopen by cmb
| From: | cmb@php.net | Date: | Sun, 07 Jul 2019 07:57:08 +0000 |
| Subject: | note 76468 deleted from function.fsockopen by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211502@lists.php.net to get a copy of this message | ||
Note Submitter: warezthebeef at gmail dot com
----
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!