Files
youlegames/codes/agent/game/api/tests/unit/test_curl_fix.php
2026-03-15 01:27:05 +08:00

103 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 测试CURL CURLOPT_SAFE_UPLOAD修复
* 模拟微信API调用验证PHP8兼容性问题已解决
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "=== CURL CURLOPT_SAFE_UPLOAD 修复测试 ===\n";
// 测试CURL基本功能
if (!function_exists('curl_init')) {
echo "❌ CURL扩展未安装\n";
exit(1);
}
echo "✅ CURL扩展已安装\n";
echo "PHP版本: " . PHP_VERSION . "\n";
// 测试CURLOPT_SAFE_UPLOAD常量是否还存在
if (defined('CURLOPT_SAFE_UPLOAD')) {
echo "⚠️ CURLOPT_SAFE_UPLOAD常量仍然存在\n";
} else {
echo "✅ CURLOPT_SAFE_UPLOAD常量已被移除PHP8正常行为\n";
}
// 测试修复后的逻辑
$ch = curl_init();
echo "✅ curl_init() 成功\n";
try {
// 模拟修复后的代码逻辑
if (version_compare(phpversion(), '5.6') >= 0 && version_compare(phpversion(), '8.0') < 0) {
echo "🔧 PHP版本 < 8.0,应该设置 CURLOPT_SAFE_UPLOAD\n";
if (defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
echo "✅ 设置 CURLOPT_SAFE_UPLOAD = false 成功\n";
}
} else {
echo "✅ PHP版本 >= 8.0,跳过 CURLOPT_SAFE_UPLOAD 设置(安全上传默认启用)\n";
}
// 测试一个简单的HTTP请求
curl_setopt($ch, CURLOPT_URL, "https://httpbin.org/get");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo "🔄 测试HTTP请求...\n";
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
if ($response !== false && $httpCode == 200) {
echo "✅ HTTP请求成功状态码: $httpCode\n";
echo "✅ CURL功能正常工作\n";
} else {
echo "⚠️ HTTP请求失败状态码: $httpCode\n";
if ($error) {
echo "错误信息: $error\n";
}
}
} catch (Exception $e) {
echo "❌ 测试失败: " . $e->getMessage() . "\n";
} catch (ValueError $e) {
echo "❌ PHP8 ValueError: " . $e->getMessage() . "\n";
echo "这表明CURLOPT_SAFE_UPLOAD问题仍然存在\n";
} finally {
curl_close($ch);
}
echo "\n=== 包含communication.func.php测试 ===\n";
// 测试实际的communication.func.php文件
require_once __DIR__ . '/framework/function/communication.func.php';
try {
echo "✅ communication.func.php 加载成功\n";
// 测试ihttp_get函数这会触发CURLOPT_SAFE_UPLOAD相关代码
if (function_exists('ihttp_get')) {
echo "🔄 测试ihttp_get函数...\n";
$result = ihttp_get("https://httpbin.org/get");
if ($result && is_array($result)) {
echo "✅ ihttp_get 函数工作正常\n";
} else {
echo "⚠️ ihttp_get 函数返回异常\n";
}
} else {
echo "❌ ihttp_get 函数不存在\n";
}
} catch (Exception $e) {
echo "❌ communication.func.php 测试失败: " . $e->getMessage() . "\n";
} catch (ValueError $e) {
echo "❌ communication.func.php PHP8 ValueError: " . $e->getMessage() . "\n";
}
echo "\n=== 测试完成 ===\n";
?>