使用PHP+HTML集成DeepSeek API,实现一个简单的聊天对话项目

项目效果预览

image-20250305143337308

1. 准备工作

  • PHP环境:确保你的开发环境中安装了PHP。
  • DeepSeek API密钥:注册并获取DeepSeek API的访问密钥。

2. 创建PHP文件,编写API调用脚本

创建一个PHP脚本(api.php),用于调用DeepSeek API。假设API密钥为YOUR_API_KEY

<?php
header('Content-Type: application/json');

function callDeepSeekAPI($message) {
   
   
    $apiKey = 'YOUR_API_KEY';
    $url = 'https://api.deepseek.com/chat/completions';

    $data = [
        "model" => "deepseek-chat",
        'messages' => [
            ["role" => "system", "content" => "You are a helpful assistant."],
            ["role" => "user", "content" => $message]
        ],
        'stream'=>false
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $apiKey
    ]);

    $response = curl_exec($ch);
    if (curl_errno($ch)) {
   
   
        $error_msg = curl_error($ch);
        curl_close(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值