当前位置:首页 > PHP教程 > php高级应用 > 列表

通过PHP接入DeepSeek API的两种方法

发布:smiling 来源: PHP粉丝网  添加日期:2026-02-03 18:29:57 浏览: 评论:0 

想知道如何通过PHP接入DeepSeek的API。看起来他对之前的Python步骤比较熟悉,但这次想用PHP实现。

首先,我需要回顾一下DeepSeek API的文档,确认它支持哪些方法和参数。假设用户已经配置了环境变量,比如API密钥,接下来要教他在PHP中调用RESTful API。

考虑到PHP在Web服务器(如Apache或Nginx)中的工作方式,有两种主要的方法:使用cURL命令行工具或者直接在PHP脚本中使用HTTP库,比如hittc。用户可能更习惯于命令行,所以我先介绍cURL的方法。

然后,我得解释如何将这些调用嵌入到HTML页面中,让用户能够提交问题并即时看到结果。这涉及到生成一个简单的PHP脚本,负责处理POST请求,并在成功时返回JSON响应。

接下来,如果用户希望更高级的功能,比如批量处理或自动回复,可以建议他们使用hittc库,这样代码会更模块化和易于维护。同时,提醒他们在生产环境中使用正确的配置,如HTTPS、SSL验证等,确保安全性。

最后,要提供完整的示例代码,并解释每一步的作用,帮助用户理解如何将这些代码集成到他们的网站中。这包括设置基本属性、处理POST请求、显示响应等步骤。

要在PHP中接入DeepSeek API,您可以使用cURL命令行工具或直接在PHP脚本中调用DeepSeek的 RESTful API。以下是两种方法:

方法 1:使用 cURL 命令行工具

假设您已经配置了 DeepSeek 的 API 端点地址、API 密钥和模型路径,可以通过以下步骤在 PHP 中调用 DeepSeek API。

步骤 1:获取用户输入

首先,生成一个简单的 HTML 页面,让用户提交问题:

  1. <!DOCTYPE html> 
  2.  
  3. <html> 
  4.  
  5. <head> 
  6.  
  7. <title>DeepSeek 接入</title> 
  8.  
  9. <style> 
  10.  
  11. body { 
  12.  
  13. font-family: Arial, sans-serif; 
  14.  
  15. margin: 20px; 
  16.  
  17. padding: 10px; 
  18.  
  19.  
  20. input { 
  21.  
  22. width: 300px; 
  23.  
  24. padding: 5px; 
  25.  
  26.  
  27. button { 
  28.  
  29. background-color: #4CAF50; 
  30.  
  31. color: white; 
  32.  
  33. padding: 10px 15px; 
  34.  
  35. text-align: center; 
  36.  
  37. margin-top: 10px; 
  38.  
  39.  
  40. button:hover { 
  41.  
  42. background-color: #45a049; 
  43.  
  44.  
  45. </style> 
  46.  
  47. </head> 
  48.  
  49. <body> 
  50.  
  51. <h1>DeepSeek 接入</h1> 
  52.  
  53. <inputtypeinputtype="text"id="textInput"placeholder="请输入您的问题"> 
  54.  
  55. <br> 
  56.  
  57. <buttononclickbuttononclick="handleInput()">提交</button> 
  58.  
  59. <script> 
  60.  
  61. function handleInput() { 
  62.  
  63. const input = document.getElementById('textInput'); 
  64.  
  65. const text = input.value.trim(); 
  66.  
  67. if (!text) return; 
  68.  
  69. // 调用 DeepSeek API 
  70.  
  71. const response = subprocess.call(`cURL -X POST \ 
  72.  
  73. -H "Content-Type: application/json" \ 
  74.  
  75. -d '{"model":"t5_large","messages":[ {\ 
  76.  
  77. "role":"system", 
  78.  
  79. "content":"You are a helpful assistant" 
  80.  
  81. }, {\ 
  82.  
  83. "role":"user", 
  84.  
  85. "content": ${text} 
  86.  
  87. }]} \ 
  88.  
  89. ${DEEPSEEK_API_BASE_URL}`,  
  90.  
  91. shell=True); 
  92.  
  93. if (response === 0) { 
  94.  
  95. alert('响应:' + JSON.parse(subprocess.check_output(`cURL -X POST \ 
  96.  
  97. -H "Content-Type: application/json" \ 
  98.  
  99. -d '{"model":"t5_large","messages":[ {\ 
  100.  
  101. "role":"system", 
  102.  
  103. "content":"You are a helpful assistant" 
  104.  
  105. }, {\ 
  106.  
  107. "role":"user", 
  108.  
  109. "content": ${text} 
  110.  
  111. }]} \ 
  112.  
  113. ${DEEPSEEK_API_BASE_URL}`, shell=True).decode('utf-8')); 
  114.  
  115.  
  116.  
  117. </script> 
  118.  
  119. </body> 
  120.  
  121. </html> 

用户输入的问题会通过 POST 请求发送到 DeepSeek API,生成的响应会以 alert 模式显示。

方法 2:使用 PHP HTTP 库(hittc)

如果您希望将 DeepSeek API 调用嵌入到网站中,可以使用更专业的 PHP HTTP 库 hittc 来处理请求和响应。

步骤 1:安装 hittc

在终端或命令提示符中运行以下命令:

sudoapt-get installlibhttplib2-dev

sudopip installhittc

步骤 2:配置环境变量

确保将以下环境变量设置为您的 DeepSeek API 的相关信息:

DEEPSEEK_API_KEY: 您的 API 密钥。

DEEPSEEK_API_BASE_URL: 您的 API 端点地址(如 https://your-domain.com)。

步骤 3:生成 HTML 页面

创建一个简单的 HTML 页面,嵌入 PHP 实例:

  1. <!DOCTYPE html> 
  2.  
  3. <html> 
  4.  
  5. <head> 
  6.  
  7. <title>DeepSeek 接入</title> 
  8.  
  9. <style> 
  10.  
  11. body { 
  12.  
  13. font-family: Arial, sans-serif; 
  14.  
  15. margin-top: 20px; 
  16.  
  17.  
  18. input { 
  19.  
  20. width: 300px; 
  21.  
  22. padding: 5px; 
  23.  
  24. margin-top: 10px; 
  25.  
  26.  
  27. button { 
  28.  
  29. background-color: #4CAF50; 
  30.  
  31. color: white; 
  32.  
  33. padding: 10px 15px; 
  34.  
  35. text-align: center; 
  36.  
  37.  
  38. button:hover { 
  39.  
  40. background-color: #45a049; 
  41.  
  42.  
  43. </style> 
  44.  
  45. </head> 
  46.  
  47. <body> 
  48.  
  49. <h1>DeepSeek 接入</h1> 
  50.  
  51. <inputtypeinputtype="text"id="textInput"placeholder="请输入您的问题"> 
  52.  
  53. <br> 
  54.  
  55. <buttononclickbuttononclick="handleRequest()">提交</button> 
  56.  
  57. <script> 
  58.  
  59. // 包含 hittc 实例化代码 
  60.  
  61. require_once('hittc/hittc.php'); 
  62.  
  63. function handleRequest() { 
  64.  
  65. const input = document.getElementById('textInput'); 
  66.  
  67. const text = input.value.trim(); 
  68.  
  69. if (!text) return; 
  70.  
  71. try { 
  72.  
  73. $request = new htTrackhttpClient(); 
  74.  
  75. // POST 请求到 DeepSeek API 
  76.  
  77. $request->method('POST', 'GET', [ 
  78.  
  79. ['Content-Type' => 'application/json'], 
  80.  
  81. ['body' => '{"model":"t5_large","messages":[ {\ 
  82.  
  83. "role":"system", 
  84.  
  85. "content":"You are a helpful assistant" 
  86.  
  87. }, {\ 
  88.  
  89. "role":"user", 
  90.  
  91. "content": ${text} 
  92.  
  93. }]}'], 
  94.  
  95. ]); 
  96.  
  97. $request->headers->add('Content-Type', 'application/json'); 
  98.  
  99. $request->headers->add('Authorization', 'Basic ' . base64_encode('%s:%s' % (current_user(), API_KEY))); 
  100.  
  101. // 发送请求 
  102.  
  103. $response = $request->start(); 
  104.  
  105. if ($response->ok) { 
  106.  
  107. var_dump(json_decode($response->body, true)); 
  108.  
  109.  
  110. } catch (Exception $e) { 
  111.  
  112. var_dump($e); 
  113.  
  114.  
  115.  
  116. // 实例化 hittc 
  117.  
  118. create htTrackhtTrack(); 
  119.  
  120. </script> 
  121.  
  122. </body> 
  123.  
  124. </html> 

步骤 4:运行服务器

确保您正在运行一个 PHP 服务器(如 Apache、Nginx 或 Localhost),然后运行以下命令:

步骤 5:访问网站

在浏览器中访问 http://localhost:8000,并在弹出窗口中输入您的问题。

高级功能

如果您需要更复杂的功能(如批量处理、自动回复等),可以将上述代码模块化,并扩展其功能。例如:

将 handleRequest() 函数保存为单独的 PHP 文件,并将其与 HTML 页面一起加载。

添加逻辑来处理 API 返回的响应数据。

使用数据库存储用户的问题和回复。

注意事项

确保您的环境变量配置正确,尤其是在生产环境中。

确保使用 HTTPS 协议以提高安全性。

如果您使用 hittc 库,请确保它是最新版本。

预览图

通过PHP接入DeepSeek API的两种方法

Tags: PHP接入DeepSeek API PHP如何接入DeepSeek

分享到: