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

WordPress插件file_get_contents SSL证书验证失败问题求助

Fixing SSL Certificate Error with file_get_contents in Your WordPress Plugin

Hey there, let's work through this SSL certificate issue you're hitting. The error you're seeing happens because file_get_contents() is trying to make an HTTPS request to fetch your plugin's local HTML file, and the SSL verification is failing—even though your host says the certificate is fine. Here are the best ways to fix this:

The simplest and most efficient fix is to stop using a URL to access your plugin's internal file. Since PRTHSEL_Visualizer.html is part of your plugin, you can read it directly from the server's file system instead of making an HTTP request.

Replace your existing code with this:

// Get the local server path to your plugin directory
$file_path = plugin_dir_path(__FILE__) . '../PRTHSEL_Visualizer.html';
// Read the file directly from the filesystem
$op = file_get_contents($file_path);

Why this works:

plugin_dir_path(__FILE__) returns the absolute local path to the directory containing your current PHP file (e.g., /var/www/html/wp-content/plugins/your-plugin/). By appending the relative path to your HTML file, you're accessing it directly on the server—no HTTP/HTTPS request needed, so SSL errors are completely avoided. This is also faster than making a web request to your own server.

If for some reason you absolutely need to use an HTTPS URL (though this doesn't make sense for a local plugin file), you can disable SSL verification for the request. Note that this reduces security, so only use this for testing or temporary fixes.

Here's how to do it:

$url = plugins_url('../PRTHSEL_Visualizer.html', __FILE__);

// Create a stream context to disable SSL checks
$ssl_context = stream_context_create([
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
    ]
]);

// Fetch the URL with the custom context
$op = file_get_contents($url, false, $ssl_context);

Important Note:

Disabling SSL verification means your plugin won't check if the certificate is valid, which opens up potential security risks (like man-in-the-middle attacks). Stick with the first solution for production environments.

Why the Original Code Failed

When you use plugins_url() to generate an HTTPS URL and then call file_get_contents() on it, your server is making an HTTPS request to itself. Sometimes, server configurations cause the local SSL verification to fail (even if the public certificate is valid)—this could be due to missing intermediate certificates, or the server not recognizing its own certificate properly. Using the local file path completely bypasses this problem.

内容的提问来源于stack exchange,提问作者Husbandman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:09:35