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

Unity调用线上PHP脚本报错:Received no data in response及Curl错误52

问题:UnityWebRequest调用PHP脚本返回空响应(Curl error 52)

直接访问https://lazyboy.wuaze.com/connect.php能正常显示"connected to DB",但在Unity中使用以下脚本调用时,控制台报Curl error 52: Empty reply from server,同时提示'Received no data in response'。

相关代码

C# 脚本

public IEnumerator connect()
{
    WWWForm form = new WWWForm();
    using (UnityWebRequest www = UnityWebRequest.Get("https://lazyboy.wuaze.com/connect.php"))
    {
        yield return www.SendWebRequest();
        if (www.isNetworkError || www.isHttpError)
        {
            print(www.error);
        }
        else
        {
            print(www.downloadHandler.text);
            byte[] results = www.downloadHandler.data;
        }
    }
}

PHP 脚本

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$servername = '';
$username = '';
$password = '';
$dbname = '';
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
else{
    echo "connected to DB";
}
?>

排查与解决方法

  • 检查HTTPS证书验证:Unity默认严格验证HTTPS证书,若服务器证书不被信任会拦截请求。可临时添加证书忽略代码测试(正式环境禁用):

    using (UnityWebRequest www = UnityWebRequest.Get("https://lazyboy.wuaze.com/connect.php"))
    {
        // 添加证书忽略逻辑
        www.certificateHandler = new IgnoreCertificateHandler();
        yield return www.SendWebRequest();
        // 后续逻辑不变
    }
    

    自定义证书处理类代码:

    public class IgnoreCertificateHandler : CertificateHandler
    {
        protected override bool ValidateCertificate(byte[] certificateData)
        {
            return true;
        }
    }
    
  • 设置合法User-Agent:部分服务器会拦截非浏览器标识的请求,手动设置User-Agent模拟浏览器:

    www.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36");
    
  • 强制PHP输出内容:避免PHP缓冲导致内容未正常发送,在echo后添加刷新命令:

    else{
        echo "connected to DB";
        flush();
        ob_flush();
    }
    
  • 检查Unity网络配置:进入Edit > Project Settings > Player > Other Settings > Configuration,确认Allow downloads over HTTPS已启用,同时检查是否有代理设置干扰请求。

  • 查看服务器日志:检查服务器的访问日志和错误日志,确认Unity的请求是否到达服务器,以及服务器返回的状态码和错误信息,定位是否是服务器端的拦截或配置问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 19:48:11