'Missing target URL parameter']); exit; } // 测试API调用 if (isset($_GET['test']) && $_GET['test'] == '1') { $test_body = isset($_POST['test_body']) ? $_POST['test_body'] : ''; $test_headers = isset($_POST['test_headers']) ? $_POST['test_headers'] : ''; if (empty($test_body) || empty($test_headers)) { echo "缺少测试数据"; exit; } // 解析JSON数据 $body_data = json_decode($test_body, true); $headers_data = json_decode($test_headers, true); echo " API测试结果

Coze API 测试结果

请求信息

URL: $url

方法: POST

请求头:

";
        foreach ($headers_data as $name => $value) {
            // 隐藏部分token以保护安全
            if ($name === 'Authorization' && strlen($value) > 20) {
                $maskedValue = substr($value, 0, 10) . '...' . substr($value, -10);
                echo "$name: $maskedValue\n";
            } else {
                echo "$name: $value\n";
            }
        }
        echo "

请求体:

" . json_encode($body_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "
"; echo "

响应信息

"; try { // 检查cURL是否可用 if (!function_exists('curl_init')) { throw new Exception('PHP cURL扩展未启用,请在php.ini中启用cURL扩展'); } // 设置cURL选项 $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 增加超时时间 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $test_body); // 设置请求头 $curl_headers = []; foreach ($headers_data as $name => $value) { $curl_headers[] = "$name: $value"; } curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers); // 获取响应头信息 curl_setopt($ch, CURLOPT_HEADER, true); // 执行请求 $response = curl_exec($ch); // 检查错误 if ($response === false) { $error = curl_error($ch); $errno = curl_errno($ch); curl_close($ch); throw new Exception("cURL错误($errno): $error"); } // 获取头信息大小 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); // 分割响应头和响应体 $header = substr($response, 0, $header_size); $body = substr($response, $header_size); $info = curl_getinfo($ch); curl_close($ch); echo "

HTTP状态码: = 200 && $info['http_code'] < 300 ? 'success' : 'error') . "'>" . $info['http_code'] . "

"; echo "

响应时间: " . $info['total_time'] . " 秒

"; echo "

响应头:

";
            $headerLines = explode("\r\n", $header);
            foreach ($headerLines as $line) {
                if (!empty($line)) {
                    echo htmlspecialchars($line) . "\n";
                }
            }
            echo "
"; // 检查是否是流式响应 $isStream = isset($body_data['stream']) && $body_data['stream'] === true; if ($isStream) { echo "

流式响应体 (解析后):

"; echo "
"; echo "

解析流式数据...

"; echo "
"; echo ""; } else { echo "

响应体:

"; // 尝试格式化JSON $json_response = json_decode($body, true); echo "
";
                if ($json_response !== null) {
                    echo json_encode($json_response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
                } else {
                    echo htmlspecialchars($body);
                }
                echo "
"; } } catch (Exception $e) { echo "

错误

"; echo "

" . $e->getMessage() . "

"; // 提供更多调试帮助 echo "

可能原因和解决方案

"; echo ""; } echo "
"; exit; } // 输出调试信息 if (isset($_GET['debug']) && $_GET['debug'] == '1') { // 获取调试数据 $debug_body = isset($_POST['debug_body']) ? $_POST['debug_body'] : ''; $debug_headers = isset($_POST['debug_headers']) ? $_POST['debug_headers'] : ''; // 解析JSON数据 $body_data = !empty($debug_body) ? json_decode($debug_body, true) : []; $headers_data = !empty($debug_headers) ? json_decode($debug_headers, true) : []; echo "

PHP代理调试信息

"; echo "

PHP版本: " . phpversion() . "

"; echo "

目标URL: " . $url . "

"; echo "

请求方法: " . $_SERVER['REQUEST_METHOD'] . "

"; echo "

浏览器请求头:

";
        foreach (getallheaders() as $name => $value) {
            echo "$name: $value\n";
        }
        echo "
"; if (!empty($headers_data)) { echo "

将发送给API的请求头:

";
            foreach ($headers_data as $name => $value) {
                echo "$name: $value\n";
            }
            echo "
"; } echo "

浏览器请求体:

";
        echo file_get_contents('php://input');
        echo "
"; if (!empty($body_data)) { echo "

将发送给API的请求体:

";
            echo json_encode($body_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
            echo "
"; } // 如果提供了完整数据,显示测试按钮 if (!empty($debug_body) && !empty($debug_headers)) { echo "
"; echo ""; echo ""; echo ""; echo "
"; } exit; } // 读取请求体 $postData = file_get_contents('php://input'); try { // 检查cURL是否可用 if (!function_exists('curl_init')) { throw new Exception('PHP cURL扩展未启用,请在php.ini中启用cURL扩展'); } // 设置cURL选项 $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 处理请求方法 if ($_SERVER['REQUEST_METHOD'] === 'POST') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); } // 设置请求头 $headers = []; foreach (getallheaders() as $name => $value) { if ($name != 'Host' && $name != 'Origin' && $name != 'Referer') { $headers[] = "$name: $value"; } } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 执行请求 $response = curl_exec($ch); // 检查错误 if ($response === false) { $error = curl_error($ch); $errno = curl_errno($ch); curl_close($ch); throw new Exception("cURL错误($errno): $error"); } $info = curl_getinfo($ch); curl_close($ch); // 设置响应头 if (isset($info['content_type'])) { header('Content-Type: ' . $info['content_type']); } header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); header('Access-Control-Allow-Headers: Content-Type, Authorization'); // 处理OPTIONS请求(预检请求) if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { header('HTTP/1.1 200 OK'); exit; } // 输出响应 echo $response; } catch (Exception $e) { header('HTTP/1.1 500 Internal Server Error'); header('Content-Type: application/json'); echo json_encode(['error' => $e->getMessage()]); } exit; } // 添加PHP检测 $isPhp = (strpos(strtolower($_SERVER['SERVER_SOFTWARE'] ?? ''), 'php') !== false) || (function_exists('php_sapi_name') && php_sapi_name() !== false); ?> Coze智能体API调用

Coze智能体API调用

显示/隐藏设置