答案是更换国内镜像源可解决Composer连接超时问题。首选方法是切换至阿里云或腾讯云镜像,命令为composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/;若无效,需检查网络连通性、DNS设置(如改用8.8.8.8)、配置代理(适用于公司网络)或延长超时时间至300秒,并清除缓存重试。

出现 "failed to open stream: Connection timed out" 错误通常是因为 Composer 在尝试下载包时无法连接到远程服务器,可能是网络不稳定、DNS 问题、代理设置或镜像源不可达导致的。以下是几种有效的解决方法:
设置全局镜像:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
或者使用腾讯云镜像:
composer config -g repo.packagist composer https://mirrors.cloud.tencent.com/composer/
这会将所有项目的 Packagist 源替换为国内镜像,大幅提升下载速度和稳定性。
尝试在终端运行:
ping github.com
curl -I https://packagist.org
如果这些命令也超时,说明本地网络有问题。可尝试:
设置代理:
composer config -g http-proxy http://proxy.example.com:8080
如果不需要代理了,记得清除:
composer config -g --unset http-proxy
增加超时时间(单位:秒):
composer config -g process-timeout 300
composer config -g http-timeout 60
这样 Composer 在执行命令或下载文件时会等待更久,减少因临时卡顿导致的失败。
清除 Composer 缓存:
composer clear-cache
然后重新运行安装命令:
composer install
以上就是composer如何修复 “failed to open stream: Connection timed out” 网络连接问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号