PHP fsockopen : How to connect to an external IPv6 address ?
Answers to your questions / Web Hosting / PHP fsockopen : How to co...
BMPCreated with Sketch.BMPZIPCreated with Sketch.ZIPXLSCreated with Sketch.XLSTXTCreated with Sketch.TXTPPTCreated with Sketch.PPTPNGCreated with Sketch.PNGPDFCreated with Sketch.PDFJPGCreated with Sketch.JPGGIFCreated with Sketch.GIFDOCCreated with Sketch.DOC Error Created with Sketch.
Question

PHP fsockopen : How to connect to an external IPv6 address ?

by
JeromeV14
Created on 2023-01-03 09:09:10 (edited on 2024-09-04 14:25:53) in Web Hosting

Hello,

In a PHP script running on my OVHCloud "Hébergement Perso", I use fsockopen towards an external server. When I use the IPv4 address of that server, my script works fine. That server (an RPi behind SFR box) will lose soon its IPv4 address and I need now to use its IPv6 address in my script.

I tried several syntax with the IPv6 address:
$fp = fsockopen("2a02:...4]", 1042, $errNo, $errStr, 10);
$fp = fsockopen("2a02:...4", 1042, $errNo, $errStr, 10);
$fp = fsockopen("tcp://[2a02:...4]", 1042, $errNo, $errStr, 10);
etc
But they all fail, PHP (PHP7.3, PHP8.0, ...) says :
php_network_getaddresses: getaddrinfo for 2a02:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxx4 failed: Address family for hostname not supported (0)

My issue is similar to [this past one
but there is no solution posted there.

I'm sure the IPv6 address is correct, as I can successfully connect to it with netcat from anywhere.

Thanks for your help,
Jerome