我的本地mac主机无法以任何形式将PHP的文件流包装器用于HTTP请求(这需要使用作曲器等)。
如果我使用curl驱动程序请求完全相同的资源,那么一切都很好。这是我直接从PHP文档中复制的两个程序:
这是卷曲的,它每次都能正常工作。
<?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;
此命令使用fopen,并且在php.ini
中的默认60秒后总是失败。此失败与file_get_contents或任何其他基于文件流的方法相同。
<?php
$opts = [ 'http'=> [ 'method' => "GET" ]];
$context = stream_context_create($opts);
$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
结果是:
Warning: fopen(http://www.example.com):
failed to open stream:
Operation timed out in ...php on line ...
我已经尝试过PHP 7.1和PHP 7.3。
这是我的ini文件的示例,其设置减少了:
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
对于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
一些事实:
brew
安装了它。allow_url_fopen
是On
$fp = fsockopen("1.1.1.1", 80, $errno, $errstr, 30);
连接,也存在相同的延迟。php -r "copy('http://54.36.53.46', 'composer-setup.php');"
时,会返回相同的超时失败。