想知道如何通过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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

<!DOCTYPE html>

<html>

<head>

    <title>DeepSeek 接入</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            margin: 20px;

            padding: 10px;

        }

        input {

            width: 300px;

            padding: 5px;

        }

        button {

            background-color: #4CAF50;

            color: white;

            padding: 10px 15px;

            text-align: center;

            margin-top: 10px;

        }

        button:hover {

            background-color: #45a049;

        }

    </style>

</head>

<body>

    <h1>DeepSeek 接入</h1>

    <input type="text" id="textInput" placeholder="请输入您的问题">

    <br>

    <button onclick="handleInput()">提交</button>

    <script>

        function handleInput() {

            const input = document.getElementById('textInput');

            const text = input.value.trim();

             

            if (!text) return;

            // 调用 DeepSeek API

            const response = subprocess.call(`cURL -X POST \

                -H "Content-Type: application/json" \

                -d '{"model":"t5_large","messages":[ {\

                    "role":"system",

                    "content":"You are a helpful assistant"

                }, {\

                    "role":"user",

                    "content": ${text}

                }]} \

                ${DEEPSEEK_API_BASE_URL}`,

                shell=True);

            if (response === 0) {

                alert('响应:' + JSON.parse(subprocess.check_output(`cURL -X POST \

                -H "Content-Type: application/json" \

                -d '{"model":"t5_large","messages":[ {\

                    "role":"system",

                    "content":"You are a helpful assistant"

                }, {\

                    "role":"user",

                    "content": ${text}

                }]} \

                ${DEEPSEEK_API_BASE_URL}`, shell=True).decode('utf-8'));

            }

        }

    </script>

</body>

</html>

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

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

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

步骤 1:安装 hittc

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

1

2

sudo apt-get install libhttplib2-dev

sudo pip install hittc

步骤 2:配置环境变量

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

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

步骤 3:生成 HTML 页面

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

<!DOCTYPE html>

<html>

<head>

    <title>DeepSeek 接入</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            margin-top: 20px;

        }

        input {

            width: 300px;

            padding: 5px;

            margin-top: 10px;

        }

        button {

            background-color: #4CAF50;

            color: white;

            padding: 10px 15px;

            text-align: center;

        }

        button:hover {

            background-color: #45a049;

        }

    </style>

</head>

<body>

    <h1>DeepSeek 接入</h1>

    <input type="text" id="textInput" placeholder="请输入您的问题">

    <br>

    <button onclick="handleRequest()">提交</button>

    <script>

        // 包含 hittc 实例化代码

        require_once('hittc/hittc.php');

        function handleRequest() {

            const input = document.getElementById('textInput');

            const text = input.value.trim();

            if (!text) return;

            try {

                $request = new htTrackhttpClient();

                 

                // POST 请求到 DeepSeek API

                $request->method('POST', 'GET', [

                    ['Content-Type' => 'application/json'],

                    ['body' => '{"model":"t5_large","messages":[ {\

                        "role":"system",

                        "content":"You are a helpful assistant"

                    }, {\

                        "role":"user",

                        "content": ${text}

                    }]}'],

                ]);

                $request->headers->add('Content-Type', 'application/json');

                $request->headers->add('Authorization', 'Basic ' . base64_encode('%s:%s' % (current_user(), API_KEY)));

                // 发送请求

                $response = $request->start();

                 

                if ($response->ok) {

                    var_dump(json_decode($response->body, true));

                }

            } catch (Exception $e) {

                var_dump($e);

            }

        }

        // 实例化 hittc

        create htTrackhtTrack();

    </script>

</body>

</html>

步骤 4:运行服务器

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

步骤 5:访问网站

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

高级功能

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

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

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

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

注意事项

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

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

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

预览图

到此这篇关于通过PHP接入DeepSeek API的两种方法的文章就介绍到这了

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐