#include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> #include "cJSON.h" #include "depskmainCopy.h" #include "lvgl/lvgl.h" #include <stdio.h> #include <errno.h> #include "ui_helpers.h" #include "ui_events.h" #include "ui.h" #include "ui_helpers.h" // 使用缓冲区输入输出是没问题的,需要解决循环的问题, char textbuf[128] = "who are you"; extern char * kbEntertext; char dpOut[128] = {0}; // ================== 前置声明 ================== struct MemoryStruct; struct APIResponse; char* get_api_key(); struct APIResponse call_deepseek(const char *api_key, const char *prompt); void print_token_usage(int prompt_tokens, int completion_tokens, int total_tokens); DeepSeekSession *session; char input[1024]; int prompt_tokens, completion_tokens, total_tokens; char *response; // ================== 辅助函数 ================== // libcurl回调函数 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { size_t realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *)userp; char *ptr = realloc(mem->memory, mem->size + realsize + 1); if(!ptr) { fprintf(stderr, "内存分配错误\n"); return 0; } mem->memory = ptr; memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } // 获取API密钥 char* get_api_key() { char *api_key = getenv("DEEPSEEK_API_KEY"); if (api_key && strlen(api_key) > 0) { return strdup(api_key); } printf("未找到环境变量 DEEPSEEK_API_KEY\n"); printf("请访问 https://platform.deepseek.com/api-keys 创建API密钥\n"); char *input_key = malloc(256); if (!input_key) { fprintf(stderr, "内存分配失败\n"); return NULL; } printf("请输入您的DeepSeek API密钥: "); if (fgets(input_key, 256, stdin) == NULL) { free(input_key); return NULL; } input_key[strcspn(input_key, "\n")] = 0; return input_key; } // ================== 核心API函数 ================== // 调用DeepSeek API struct APIResponse call_deepseek(const char *api_key, const char *prompt) { CURL *curl; CURLcode res; struct MemoryStruct chunk = {0}; long http_code = 0; struct APIResponse api_response = {0}; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(!curl) { fprintf(stderr, "无法初始化cURL\n"); return api_response; } // 设置API端点 curl_easy_setopt(curl, CURLOPT_URL, "https://api.deepseek.com/chat/completions"); // 设置证书路径 // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // // 添加 SSL 版本和密码套件配置 // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "HIGH:!aNULL:!MD5"); // 设置 TLS 1.3 // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_3); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "TLS_AES_128_GCM_SHA256"); // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "DEFAULT@SECLEVEL=1"); // 添加SSL证书验证配置,禁用证书验证(仅用于测试) // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // 设置完整的证书链 // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // curl_easy_setopt(curl, CURLOPT_CAPATH, "/etc/ssl/certs"); // 配置SSL证书和验证选项 curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "DEFAULT"); curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/new_combined_certs.pem"); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // 验证配置 // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // 添加更多调试信息 // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // 添加详细的错误信息 char errbuf[CURL_ERROR_SIZE]; curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L); // 设置请求头 struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); char auth_header[256]; snprintf(auth_header, sizeof(auth_header), "Authorization: Bearer %s", api_key); headers = curl_slist_append(headers, auth_header); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 构建请求体 cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "model", "deepseek-chat"); cJSON_AddNumberToObject(root, "max_tokens", 2000); cJSON *messages = cJSON_CreateArray(); cJSON *message = cJSON_CreateObject(); cJSON_AddStringToObject(message, "role", "user"); cJSON_AddStringToObject(message, "content", prompt); cJSON_AddItemToArray(messages, message); cJSON_AddItemToObject(root, "messages", messages); char *post_data = cJSON_PrintUnformatted(root); // 设置POST数据 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(post_data)); // 设置响应回调 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); // 设置超时 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); // 执行请求 res = curl_easy_perform(curl); // 检查不支持的协议 if(res == CURLE_UNSUPPORTED_PROTOCOL) { fprintf(stderr, "cURL错误: 不支持的协议 - URL: %s\n", "https://api.deepseek.com/chat/completions"); // 获取支持的协议列表 curl_version_info_data *ver_info = curl_version_info(CURLVERSION_NOW); if(ver_info && ver_info->protocols) { fprintf(stderr, "支持的协议列表:\n"); const char * const *proto; for(proto = ver_info->protocols; *proto; proto++) { fprintf(stderr, "- %s\n", *proto); } } } else if(res != CURLE_OK) { fprintf(stderr, "cURL错误: %s\n", curl_easy_strerror(res)); fprintf(stderr, "详细错误: %s\n", errbuf); long connect_code; curl_easy_getinfo(curl, CURLINFO_HTTP_CONNECTCODE, &connect_code); fprintf(stderr, "连接状态码: %ld\n", connect_code); } else if (res == CURLE_SSL_PEER_CERTIFICATE) { fprintf(stderr, "SSL证书错误: %s\n", curl_easy_strerror(res)); // 获取更多错误信息 char error_buffer[CURL_ERROR_SIZE]; curl_easy_getinfo(curl, CURLINFO_SSL_VERIFYRESULT, &error_buffer); fprintf(stderr, "SSL验证结果: %s\n", error_buffer); } // 检查HTTP状态码 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); // 清理cURL资源 curl_easy_cleanup(curl); curl_slist_free_all(headers); cJSON_Delete(root); free(post_data); // 调试信息 printf("\n--- API请求详情 ---\n"); printf("端点: https://api.deepseek.com/chat/completions\n"); printf("模型: deepseek-chat\n"); printf("提示: %s\n", prompt); printf("状态码: %ld\n", http_code); if(res != CURLE_OK) { fprintf(stderr, "cURL错误: %s\n", curl_easy_strerror(res)); if(chunk.memory) free(chunk.memory); curl_global_cleanup(); return api_response; } // 检查HTTP状态码 if(http_code != 200) { fprintf(stderr, "API错误: HTTP %ld\n", http_code); if(chunk.memory) { printf("原始响应: %s\n", chunk.memory); cJSON *error_json = cJSON_Parse(chunk.memory); if(error_json) { cJSON *error_obj = cJSON_GetObjectItem(error_json, "error"); if(error_obj) { cJSON *message = cJSON_GetObjectItem(error_obj, "message"); cJSON *code = cJSON_GetObjectItem(error_obj, "code"); if(cJSON_IsString(message)) { fprintf(stderr, "错误信息: %s\n", message->valuestring); } if(cJSON_IsString(code)) { fprintf(stderr, "错误代码: %s\n", code->valuestring); } } cJSON_Delete(error_json); } } if(chunk.memory) free(chunk.memory); curl_global_cleanup(); return api_response; } curl_global_cleanup(); // 解析API响应 cJSON *response_json = cJSON_Parse(chunk.memory); if (!response_json) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr) fprintf(stderr, "JSON解析错误: %s\n", error_ptr); fprintf(stderr, "原始响应: %s\n", chunk.memory); free(chunk.memory); return api_response; } // 提取令牌使用信息 cJSON *usage = cJSON_GetObjectItem(response_json, "usage"); if (usage) { cJSON *prompt_tokens = cJSON_GetObjectItem(usage, "prompt_tokens"); cJSON *completion_tokens = cJSON_GetObjectItem(usage, "completion_tokens"); cJSON *total_tokens = cJSON_GetObjectItem(usage, "total_tokens"); if (cJSON_IsNumber(prompt_tokens)) api_response.prompt_tokens = prompt_tokens->valueint; if (cJSON_IsNumber(completion_tokens)) api_response.completion_tokens = completion_tokens->valueint; if (cJSON_IsNumber(total_tokens)) api_response.total_tokens = total_tokens->valueint; } // 提取回复内容 cJSON *choices = cJSON_GetObjectItem(response_json, "choices"); if (cJSON_IsArray(choices) && cJSON_GetArraySize(choices) > 0) { cJSON *first_choice = cJSON_GetArrayItem(choices, 0); cJSON *message = cJSON_GetObjectItem(first_choice, "message"); cJSON *content = cJSON_GetObjectItem(message, "content"); if (cJSON_IsString(content) && content->valuestring != NULL) { api_response.content = strdup(content->valuestring); } } cJSON_Delete(response_json); free(chunk.memory); return api_response; } // 显示令牌使用信息 void print_token_usage(int prompt_tokens, int completion_tokens, int total_tokens) { printf("\n--- 令牌使用 ---\n"); printf("提示令牌: %d\n", prompt_tokens); printf("完成令牌: %d\n", completion_tokens); printf("总令牌: %d\n", total_tokens); // 估算成本 double estimated_cost = total_tokens * 0.0000015; // $0.0015/1K tokens printf("估算成本: $%.6f\n", estimated_cost); } // ================== 会话管理函数 ================== // 创建新会话 DeepSeekSession* deepseek_create_session(const char *api_key) { DeepSeekSession *session = malloc(sizeof(DeepSeekSession)); if (!session) return NULL; // 修复类型不匹配问题 if (api_key) { session->api_key = strdup(api_key); } else { session->api_key = get_api_key(); } session->total_prompt_tokens = 0; session->total_completion_tokens = 0; session->total_all_tokens = 0; return session; } // 销毁会话 void deepseek_destroy_session(DeepSeekSession *session) { if (!session) return; free(session->api_key); free(session); } // 发送消息并获取回复 char* deepseek_send_message( DeepSeekSession *session, const char *message, int *prompt_tokens, // 返回本次提示令牌 int *completion_tokens, // 返回本次完成令牌 int *total_tokens // 返回本次总令牌 ) { if (!session || !message) return NULL; struct APIResponse response = call_deepseek(session->api_key, message); if (response.content) { // 更新会话令牌计数 session->total_prompt_tokens += response.prompt_tokens; session->total_completion_tokens += response.completion_tokens; session->total_all_tokens += response.total_tokens; // 设置输出参数 if (prompt_tokens) *prompt_tokens = response.prompt_tokens; if (completion_tokens) *completion_tokens = response.completion_tokens; if (total_tokens) *total_tokens = response.total_tokens; return response.content; } return NULL; } // 获取会话令牌统计 void deepseek_get_token_stats( DeepSeekSession *session, int *total_prompt, int *total_completion, int *total_all ) { if (!session) return; if (total_prompt) *total_prompt = session->total_prompt_tokens; if (total_completion) *total_completion = session->total_completion_tokens; if (total_all) *total_all = session->total_all_tokens; } void get_full_text_input(char *dest, size_t dest_size) { // 获取文本区域内容指针 const char *text = lv_textarea_get_text(ui_TextArea1); if (!text) { dest[0] = '\0'; return; } // 计算实际文本长度(包括空格) size_t text_len = strlen(text); // 安全复制文本(防止缓冲区溢出) size_t copy_len = text_len < dest_size - 1 ? text_len : dest_size - 1; strncpy(dest, text, copy_len); dest[copy_len] = '\0'; // 确保字符串终止 // 移除末尾的换行符(如果有) char *newline = strchr(dest, '\n'); if (newline) *newline = '\0'; printf("111111111111\n"); } // ================== 主函数 ================== int depmainlong(lv_event_t * e) // 创建会话 { lv_event_code_t event_code = lv_event_get_code(e); lv_obj_t * target = lv_event_get_target(e); if( event_code == LV_EVENT_LONG_PRESSED ) { // 创建会话 - 修复API密钥传递问题 // session = deepseek_create_session("sk-28b778879e5b4fd6b227d767812fd83d"); // if (!session) { // fprintf(stderr, "创建会话失败\n"); // return 1; // } printf("DeepSeek聊天客户端 (输入'exit'退出)\n"); printf("使用的API密钥: %.6s...\n", session->api_key); // if( !lv_obj_has_flag(ui_Keyboard1, LV_OBJ_FLAG_HIDDEN)) { // printf("\n你的问题: "); // // char input[1024]; // // 如何判定提交文本时机,以及如何控制循环 // /********************* // * @brief Construct a new if object // * @details 按键长按不起作用,起作用的是点击 // * 点击一次,进入一次,判断一次。 // * 而由于全局变量的设置,文本内容与对话的循环并无直接关系 // * 目前会出现段错误,猜测可能是由于内容缓冲区越界,因为还保留了上下文 // *************************************************/ // if( event_code == LV_EVENT_CLICKED ) // { // printf("inputlogo被长按!\n"); // printf("111111111111"); // } // if(1) // { // kbEntertext = lv_textarea_get_text(ui_TextArea1); // printf("提交内容: %s\n", kbEntertext); // } // // 使用这种方式需要处理循环的问题以及刷新。 // sscanf(kbEntertext, "%s", input); // printf("kbEntertext = %s\n", kbEntertext); // printf("input = %s\n", input); // // 移除换行符 // input[strcspn(input, "\n")] = 0; // // 线程阻塞原因,暂不使用循环while 以及相应的语句 // // if(strcmp(input, "exit") == 0) break; // // if(strlen(input) == 0) continue; // printf("正在查询DeepSeek API...\n"); // // int prompt_tokens, completion_tokens, total_tokens; // // char *response = deepseek_send_message // response = deepseek_send_message( // session, // input, // &prompt_tokens, // &completion_tokens, // &total_tokens // ); // if(response) { // // printf("\n--- DeepSeek回复 ---\n%s\n", response); // printf("\n--- DeepSeek回复 ---\n"); // sprintf(dpOut, "%s", response); // printf("%s\n", dpOut); // lv_label_set_text(ui_AILabel, dpOut); // // 显示令牌使用 // print_token_usage(prompt_tokens, completion_tokens, total_tokens); // free(response); // } else { // printf("获取回复时出错\n"); // } // } return 0; } } int depmaintalk(lv_event_t * e) // 会话 { lv_event_code_t event_code = lv_event_get_code(e); lv_obj_t * target = lv_event_get_target(e); if( event_code == LV_EVENT_CLICKED ) { if( !lv_obj_has_flag(ui_Keyboard1, LV_OBJ_FLAG_HIDDEN)) { printf("\n你的问题: "); // char input[1024]; printf("inputlogo被anxia!\n"); // kbEntertext = lv_textarea_get_text(ui_TextArea1); // input = lv_textarea_get_text(ui_TextArea1); get_full_text_input(input, sizeof(input)); // sscanf(lv_textarea_get_text(ui_TextArea1), "%s", input); printf("kbEntertext = %s\n", kbEntertext); printf("input = %s\n", input); if( strcmp(input, "exit") == 0 ) { // 清理资源 deepseek_destroy_session(session); printf("退出!\n"); return 0; } // 使用这个清理判断会卡住,不知道为什么 // if( strcmp(input, "exit") ) // { // // 清理资源 // deepseek_destroy_session(session); // return 0; // } printf("正在查询DeepSeek API...\n"); // char *response = deepseek_send_message response = deepseek_send_message( session, input, &prompt_tokens, &completion_tokens, &total_tokens ); if(response) { // printf("\n--- DeepSeek回复 ---\n%s\n", response); printf("\n--- DeepSeek回复 ---\n"); sprintf(dpOut, "%s", response); printf("%s\n", dpOut); lv_label_set_text(ui_AILabel, dpOut); // 显示令牌使用 print_token_usage(prompt_tokens, completion_tokens, total_tokens); free(response); } else { printf("获取回复时出错\n"); } } if( strcmp(input, "exit") == 0 ) { // 清理资源 deepseek_destroy_session(session); return 0; } } }
时间: 2025-07-31 22:00:35 浏览: 3
<think>我们正在解决两个问题:1.修复cURL调用DeepSeekAPI时出现的`error:04091077:rsaroutines:int_rsa_verify:wrongsignaturelength`错误。2.优化代码中关于输入输出缓冲区和循环逻辑的部分。首先,针对第一个问题:RSA签名验证错误。根据之前的分析,这个错误通常是因为签名长度与RSA密钥长度不匹配。在cURL的上下文中,这可能与SSL/TLS握手过程中的证书验证有关。可能的原因包括:-服务器返回的证书链中某个证书的签名无效(长度不对)-本地CA证书包可能有问题,导致无法正确验证服务器证书引用[3]中提到PHPOpenSSL的功能和证书管理,我们可以尝试更新CA证书包。解决方案步骤:1.更新CA证书包:使用引用[3]中的方法,下载最新的CA证书包并配置PHP使用它。2.在cURL请求中明确指定CA证书路径。其次,关于优化代码的循环和输入输出缓冲区:-循环逻辑优化:避免不必要的循环,减少资源消耗。-输入输出缓冲区:合理使用缓冲区可以提高效率,尤其是在处理大量数据时。下面我们给出具体的代码示例。假设用户当前的代码可能是这样的(伪代码):```php//原始代码可能有类似这样的cURL调用$ch=curl_init();curl_setopt($ch,CURLOPT_URL,"https://api.deepseek.com/...");curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//...其他设置$response=curl_exec($ch);if(curl_errno($ch)){echo'cURLerror:'.curl_error($ch);}curl_close($ch);```修复步骤:1.更新CA证书包(如果问题是由CA证书引起的):按照引用[3]的方法:```bashcd~&&wgethttps://curl.se/ca/cacert.pem```然后在php.ini中设置:```iniopenssl.cafile=/path/to/cacert.pem```或者在代码中指定:```phpcurl_setopt($ch,CURLOPT_CAINFO,'/path/to/cacert.pem');```2.如果更新CA证书包无效,尝试跳过证书验证(仅用于测试,生产环境不推荐):```phpcurl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);```3.优化循环和缓冲区:假设用户代码中有循环调用API,我们可以考虑以下优化:-使用适当的等待时间避免频繁调用(如使用`sleep`)-使用输出缓冲区控制(`ob_start`和`ob_flush`)来管理输出,尤其是在长时间运行的脚本中示例优化后的代码:```php//设置CA证书路径$caBundle='/path/to/cacert.pem';//更新为实际路径//初始化cURL会话$ch=curl_init();curl_setopt($ch,CURLOPT_URL,"https://api.deepseek.com/...");curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_CAINFO,$caBundle);//指定CA证书//其他必要的cURL选项(如请求头、POST数据等)//执行请求$response=curl_exec($ch);if(curl_errno($ch)){echo'cURLerror:'.curl_error($ch);}else{//处理响应$data=json_decode($response,true);//...处理逻辑}curl_close($ch);//循环优化示例:假设需要多次调用,每次调用后等待1秒for($i=0;$i<10;$i++){//使用输出缓冲区控制,避免在循环中频繁输出ob_start();//执行API调用(可以将上面的代码封装成函数)call_api();//获取缓冲区内容并清空$output=ob_get_clean();//处理输出,例如写入日志或按需显示echo$output;//避免频繁调用,等待1秒sleep(1);}```注意:跳过证书验证(CURLOPT_SSL_VERIFYPEER)仅用于测试,在生产环境中应始终验证服务器证书。如果问题依然存在,请检查服务器端返回的证书链,可能是服务器配置问题。关于循环优化,我们使用了输出缓冲区来避免在循环中直接输出到浏览器(如果是Web环境),这可以减少I/O操作。同时,每次循环后等待1秒,避免请求过于频繁。如果用户代码中没有循环,而是有缓冲区问题,比如需要实时输出内容(例如长时间运行的脚本),可以使用以下方式:```php//关闭输出缓冲ob_implicit_flush(true);ob_end_flush();```但注意,这可能会导致性能下降,因为每次输出都会立即发送。总结:-更新CA证书包是解决RSA签名错误的首选方法。-优化循环时注意减少不必要的操作,合理使用缓冲和等待时间。如果问题仍未解决,请提供更多错误上下文信息。§§相关问题§§1.如何在不同操作系统中更新PHP的CA证书包?2.cURL请求中如何捕获详细的SSL握手错误?3.如何优化PHP脚本的内存使用?4.如何调试PHP中的cURL请求?5.在循环中调用API的最佳实践是什么?</think>### 解决cURL调用DeepSeek API的RSA签名错误及代码优化
#### 1. 修复RSA签名验证错误 `error:04091077`
此错误通常由SSL/TLS证书问题引起,以下是完整解决方案:
**步骤1:更新CA证书包**(解决证书链验证问题)
```bash
# 下载最新CA证书包
cd ~ && wget https://curl.se/ca/cacert.pem
# PHP中指定证书路径(代码中)
curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cacert.pem');
# 或在php.ini中全局设置
openssl.cafile = /path/to/cacert.pem
```
[^3]
**步骤2:强制使用TLSv1.2+协议**(解决SSLv3兼容问题)
```php
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
// 或更安全的
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_MAX_TLSv1_2);
```
[^2]
**步骤3:证书验证调试**
```php
// 启用详细错误日志
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('curl.log', 'w+'));
// 手动验证证书(开发环境)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 生产环境勿用!
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
```
**步骤4:清除OpenSSL错误队列**(防止错误累积)
```php
// 在每次SSL操作前清除错误
if (function_exists('openssl_clear_error_string')) {
while ($err = openssl_error_string()) {} // 清空错误队列
}
```
[^1]
#### 2. 输入输出缓冲区与循环优化
**优化1:高效缓冲区管理**
```php
ob_start(); // 开启输出缓冲
for ($i = 0; $i < $maxRequests; $i++) {
// API调用逻辑
// 分段刷新缓冲区(每N次请求)
if ($i % 5 == 0) {
ob_flush();
flush();
}
}
ob_end_clean(); // 结束时清理缓冲区
```
**优化2:智能请求间隔控制**
```php
$backoff = 100; // 初始间隔(ms)
$maxBackoff = 5000; // 最大间隔
foreach ($requests as $req) {
try {
$response = curl_exec($ch);
$backoff = max(100, $backoff * 0.9); // 成功时缩短间隔
} catch (Exception $e) {
$backoff = min($maxBackoff, $backoff * 2); // 失败时指数退避
usleep($backoff * 1000); // 微秒延时
}
}
```
**优化3:连接复用与批处理**
```php
// 启用HTTP/2多路复用
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
// 批处理请求示例
$multiHandle = curl_multi_init();
$handles = [];
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CAINFO => '/path/to/cacert.pem'
]);
curl_multi_add_handle($multiHandle, $ch);
$handles[] = $ch;
}
// 并行执行
do {
curl_multi_exec($multiHandle, $running);
curl_multi_select($multiHandle); // 减少CPU占用
} while ($running > 0);
```
#### 完整优化示例
```php
$cacert = '/etc/ssl/certs/cacert.pem';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.deepseek.com/endpoint',
CURLOPT_CAINFO => $cacert,
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10
]);
// 清空SSL错误队列
while (openssl_error_string()) {}
ob_start();
$backoff = 100;
for ($i = 0; $i < 10; $i++) {
try {
$response = curl_exec($ch);
$backoff = max(100, $backoff * 0.8);
// 处理响应...
} catch (Exception $e) {
$backoff = min(5000, $backoff * 1.5);
usleep($backoff * 1000);
}
if ($i % 3 == 0) {
ob_flush();
flush();
}
}
curl_close($ch);
ob_end_clean();
```
#### 关键优化点总结:
1. **证书管理**:使用最新CA证书包确保信任链完整
2. **协议安全**:强制TLSv1.2+避免遗留协议问题
3. **错误处理**:清除OpenSSL错误队列防止污染
4. **缓冲策略**:分段刷新输出减少内存压力
5. **重试机制**:指数退避算法避免服务过载
6. **连接复用**:HTTP/2多路复用提升吞吐量
> 提示:生产环境应始终启用证书验证(`CURLOPT_SSL_VERIFYPEER=true`),禁用验证仅限调试阶段使用。
阅读全文
相关推荐




















