You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows 10下PHP脚本无法访问SSL接口问题求助

Torn API本地PHP脚本SSL证书验证失败问题排查与解决

问题背景

在Windows 10设备上运行调用Torn API的PHP脚本(该游戏官方允许并鼓励此类API调用)。近期本地命令行及php -s启动的临时Web服务器中,所有脚本均停止工作(此前两种运行方式均正常),但部署在其他网站的同款脚本仍可正常运行,怀疑问题由近期Windows更新导致。已尝试PHP 7.4.6和8.4.4版本(CLI、NTS、x64),执行测试代码时出现SSL证书验证失败错误。

测试代码

<?php

$jsonurl = "https://api.torn.com/faction/16335?selections=basic&key=aaa";
$json = file_get_contents( $jsonurl ) ; // 获取API输出
$decodedString = json_decode( $json, true ) ; // 解析API的JSON输出
print_r( $decodedString ) ;
?>

报错信息

PHP Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in C:\Users\borre\OneDrive\work\torn\apitest.php on line 4

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in C:\Users\borre\OneDrive\work\torn\apitest.php on line 4
PHP Warning: file_get_contents(): Failed to enable crypto in C:\Users\borre\OneDrive\work\torn\apitest.php on line 4

Warning: file_get_contents(): Failed to enable crypto in C:\Users\borre\OneDrive\work\torn\apitest.php on line 4
PHP Warning: file_get_contents(https://api.torn.com/faction/16335?selections=basic&key=aaa): failed to open stream: operation failed in C:\Users\borre\OneDrive\work\torn\apitest.php on line 4

Warning: file_get_contents(https://api.torn.com/faction/16335?selections=basic&key=aaa): failed to open stream: operation failed in C:\Users\borre\OneDrive\work\torn\apitest.php on line 4

解决方案

1. 配置PHP的CA证书路径

Windows更新可能破坏了PHP默认的CA证书配置,需手动指定有效证书文件:

  • 找到PHP安装目录下的php.ini文件(CLI版本和NTS版本可能对应不同的php.ini)
  • 查找并修改以下两个配置项,指向本地的CA证书文件(如cacert.pem,可下载兼容PHP的CA证书包):
    curl.cainfo = "C:\php\extras\ssl\cacert.pem"
    openssl.cafile = "C:\php\extras\ssl\cacert.pem"
    
  • 保存修改后,重启PHP CLI会话或临时Web服务器

2. 修复Windows系统根证书

Windows更新可能导致受信任的根证书丢失或异常:

  • 按下Win+R输入certmgr.msc打开证书管理器
  • 展开「受信任的根证书颁发机构」→「证书」,检查是否包含api.torn.com证书链中的根CA(例如Let's Encrypt的根证书)
  • 如果缺失,通过浏览器访问https://api.torn.com,点击地址栏的锁图标查看证书详情,导出根证书后手动安装到系统根证书存储中

3. 临时调试方案(仅限测试,勿用于生产)

若需快速验证API功能,可临时禁用SSL证书验证(存在安全风险,仅用于排查),修改代码如下:

<?php
$jsonurl = "https://api.torn.com/faction/16335?selections=basic&key=aaa";
// 创建禁用SSL验证的上下文
$context = stream_context_create([
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false
    ]
]);
$json = file_get_contents($jsonurl, false, $context);
$decodedString = json_decode($json, true);
print_r($decodedString);
?>

内容的提问来源于stack exchange,提问作者carlo.borreo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:21:04