Incognito
Incognito

Reputation: 20765

curl works, sockets fail for most domains and IPs

My local mac host is unable to use PHP's file stream wrappers in any form for HTTP requests (which is required to use composer, etc).

If I request the exact same resource with the curl driver, everything is fine. Here's two programs I copied directly out of the PHP documentation:

This one is curl, it works every time without fail.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1)
$output = curl_exec($ch);
curl_close($ch);
echo $output;

This one uses fopen, and always fails after the 60 second default in php.ini. This failure is the same with file_get_contents or any other file stream based approach.

<?php
$opts = [ 'http'=> [ 'method' => "GET" ]];

$context = stream_context_create($opts);
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);

This results in:

Warning: fopen(http://www.example.com): 
failed to open stream: 
    Operation timed out in ...php on line ...

I've tried PHP 7.1 and PHP 7.3.

Here's a sample of my ini file with reduced settings:

allow_url_fopen = On
allow_url_include = Off
auto_append_file =
auto_globals_jit = On
auto_prepend_file =
default_charset = "UTF-8"
default_mimetype = "text/html"
default_socket_timeout = 60
display_errors = On
display_startup_errors = On
enable_dl = Off
engine = On
error_reporting = E_ALL
extension_dir = "/usr/local/lib/php/pecl/20180731"
file_uploads = On
html_errors = On
implicit_flush = Off
output_buffering = 4096

And for streams in php --info

$ php --info  | grep -i stream
Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, tls, tlsv1.0, tlsv1.1, tlsv1.2
Registered Stream Filters => zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk
Stream Wrapper support => compress.bzip2://
Stream Filter support => bzip2.decompress, bzip2.compress
libXML streams => enabled
mbstring extension makes use of "streamable kanji code filter and converter", which is distributed under the GNU Lesser General Public License version 2.1.
Stream Wrapper => compress.zlib://
Stream Filter => zlib.inflate, zlib.deflate

Some facts:

  1. I installed this with brew.
  2. The issue is present with all URLs
  3. We know it's not a firewall (curl works)
  4. allow_url_fopen is On
  5. Only happens when opening a stream
  6. Multiple installs of PHP have this issue.
  7. Even if connecting with fstreamopen $fp = fsockopen("1.1.1.1", 80, $errno, $errstr, 30); there's the same delay.
  8. If removing the domain name and using an IP like 1.1.1.1 directly, the problem is gone. However, when I request php -r "copy('http://54.36.53.46', 'composer-setup.php');" the same timeout failure returns.

Upvotes: 2

Views: 149

Answers (0)

Related Questions