当前位置: 首页 > news >正文

Deepseek PHP API调用指南

 本文将介绍如何通过 PHP 调用 Deepseek API,并通过简易代码展示如何与 Deepseek 的 AI 模型进行交互,帮助开发者更好地在自己的项目中应用这一强大的工具。我们将提供一个基本的 PHP 示例,帮助你快速了解如何通过 Deepseek API 进行调用。

以下是调用 Deepseek API 的 PHP 示例代码:

<?php
$content = file_get_contents("2025-02-12_14-48-33_2HY8G.txt");
$content = substr($content, 0, 50);  //测试代码是否正确时,可以改小一点,节省费用

$api_key = 'sk-输入apikey';

$url = 'https://api.deepseek.com/chat/completions';
$header = "Content-Type: application/json\nAuthorization: Bearer $api_key \n";

$requestObj = new stdClass();
$requestObj->model = 'deepseek-chat';

$response_format = new stdClass();

$messages = [];
$message = new stdClass();
$message->role = 'system';
$message->content = '你是环保及绿色减排行业的数据分析专家';
$messages[] = $message;

$message = new stdClass();
$message->role = 'user';
$message->content = <<<EOD
1、表格,如果碰到关键词Parameter,Uint,A1-A3,A4等,且这些关键词在同一行显示,也认为这是表格,含有这些关键词的那一行为表头,
"""$content"""
EOD;
$messages[] = $message;
$requestObj->messages = $messages;

$request = json_encode($requestObj, JSON_UNESCAPED_UNICODE);
print_r($request);

$opts = array(
    'http' => [
        'method' => 'POST',
        'header' => $header,
        'content' => $request,
        'timeout' => 300
    ],
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false
    ]
);
$context = stream_context_create($opts);
$response = @file_get_contents($url, false, $context);

//request格式不符合要求时,api会报错
if($response === false){
  var_dump($http_response_header);
  echo "\n\nerror\n";
  die();
}

$responseObj = json_decode($response);
echo "\n\n\n";
print_r($response);
echo "\n\n\n";
print_r($responseObj);
echo "\n\n\n";
echo $responseObj->choices[0]->message->content;

返回结果如下:

{"Product": "ODE ISIPAN STD XPS Insulation Materials",  "EPD registration number": "S-P-03943",  "Publication date": "16.06.2021",  "Validity date": "15.05.2026",  "Region": "Global",  "Functional unit / declared unit": "1 m²K/W",  "Tables": [    {      "Product Name": "ODE ISIPAN STD XPS Insulation Materials",      "Region": "Global",      "Product thickness": "20-80",      "Product thickness unit": "mm",      "Product mass": "0.89",      "Product mass unit": "kg",      "Ingredients": {        "Unit/FU": "percent",        "Polystyrene": "80-90",        "Blowing Agents": "<12",        "Flame Retardent": "<1",        "Nucleating Agents": "<1",        "Additives": "<1"      },      "Production waste": [],      "Production Packaging": [],      "Installation": [        {          "Row header": "Adhesive mortar",          "Row unit": "4 kg/m²"        }      ],      "Installation waste": [],      "Installation packaging": [],      "Reference Service Life (RSL)": "Not specified",      "Cleaning and maintenance": [],      "End of Life": [        {          "Row1 header": "Disposal",          "Row1 unit": "Landfill"        }      ],      "Potential Environmental Impact": [        {          "Parameter": "GWP - Total",          "Col1 Unit Header": "kg CO₂ eq",          "Col1 header": "A1-A3",          "Col1 Value": "3.44",          "Col2 header": "A4",          "Col2 Value": "0.052",          "Col3 header": "A5",          "Col3 Value": "5.62",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "0.014",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "0.823",          "Col8 header": "D",          "Col8 Value": "-0.015"        },        {          "Parameter": "ODP",          "Col1 Unit Header": "kg CFC-11 eq",          "Col1 header": "A1-A3",          "Col1 Value": "0.00011",          "Col2 header": "A4",          "Col2 Value": "1.0E-9",          "Col3 header": "A5",          "Col3 Value": "537E-9",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "3.13E-9",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "3.10E-9",          "Col8 header": "D",          "Col8 Value": "-486E-12"        },        {          "Parameter": "AP",          "Col1 Unit Header": "mol H+ eq",          "Col1 header": "A1-A3",          "Col1 Value": "0.014",          "Col2 header": "A4",          "Col2 Value": "0.001",          "Col3 header": "A5",          "Col3 Value": "0.036",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "59.9E-6",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "154E-6",          "Col8 header": "D",          "Col8 Value": "-65.9E-6"        },        {          "Parameter": "EP - Freshwater",          "Col1 Unit Header": "kg P eq",          "Col1 header": "A1-A3",          "Col1 Value": "0.001",          "Col2 header": "A4",          "Col2 Value": "3.64E-6",          "Col3 header": "A5",          "Col3 Value": "0.002",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "1.21E-6",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "14.1E-6",          "Col8 header": "D",          "Col8 Value": "-4.52E-6"        },        {          "Parameter": "EP - Marine",          "Col1 Unit Header": "kg N eq",          "Col1 header": "A1-A3",          "Col1 Value": "0.002",          "Col2 header": "A4",          "Col2 Value": "169E-6",          "Col3 header": "A5",          "Col3 Value": "0.006",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "17.6E-6",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "0.002",          "Col8 header": "D",          "Col8 Value": "-12.9E-6"        },        {          "Parameter": "POCP",          "Col1 Unit Header": "kg NMVOC",          "Col1 header": "A1-A3",          "Col1 Value": "0.009",          "Col2 header": "A4",          "Col2 Value": "0.001",          "Col3 header": "A5",          "Col3 Value": "0.021",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "58.6E-6",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "302E-6",          "Col8 header": "D",          "Col8 Value": "-60.4E-6"        },        {          "Parameter": "ADPE",          "Col1 Unit Header": "kg Sb eq",          "Col1 header": "A1-A3",          "Col1 Value": "10.6E-6",          "Col2 header": "A4",          "Col2 Value": "0.000",          "Col3 header": "A5",          "Col3 Value": "0.001",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "379E-9",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "128E-9",          "Col8 header": "D",          "Col8 Value": "-144E-9"        },        {          "Parameter": "ADPF",          "Col1 Unit Header": "MJ",          "Col1 header": "A1-A3",          "Col1 Value": "68.8",          "Col2 header": "A4",          "Col2 Value": "0.731",          "Col3 header": "A5",          "Col3 Value": "178.9",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "0.212",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "0.293",          "Col8 header": "D",          "Col8 Value": "-0.423"        },        {          "Parameter": "WDP",          "Col1 Unit Header": "m³ depriv.",          "Col1 header": "A1-A3",          "Col1 Value": "1.93",          "Col2 header": "A4",          "Col2 Value": "0.002",          "Col3 header": "A5",          "Col3 Value": "2.99",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "0.001",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "0.010",          "Col8 header": "D",          "Col8 Value": "-0.012"        }      ],      "Use of resources": [        {          "Parameter": "PERE",          "Col1 Unit Header": "MJ",          "Col1 header": "A1-A3",          "Col1 Value": "1.54",          "Col2 header": "A4",          "Col2 Value": "0.007",          "Col3 header": "A5",          "Col3 Value": "4.90",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "0.002",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "0.013",          "Col8 header": "D",          "Col8 Value": "-0.019"        },        {          "Parameter": "PENRE",          "Col1 Unit Header": "MJ",          "Col1 header": "A1-A3",          "Col1 Value": "68.8",          "Col2 header": "A4",          "Col2 Value": "0.731",          "Col3 header": "A5",          "Col3 Value": "178.9",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "0.212",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "0.293",          "Col8 header": "D",          "Col8 Value": "-0.423"        },        {          "Parameter": "FW",          "Col1 Unit Header": "m³",          "Col1 header": "A1-A3",          "Col1 Value": "0.010",          "Col2 header": "A4",          "Col2 Value": "110E-6",          "Col3 header": "A5",          "Col3 Value": "0.073",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "36.7E-6",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "269E-6",          "Col8 header": "D",          "Col8 Value": "-54.0E-6"        }      ],      "Waste & Output Flows": [        {          "Parameter": "HWD",          "Col1 Unit Header": "kg",          "Col1 header": "A1-A3",          "Col1 Value": "383E-9",          "Col2 header": "A4",          "Col2 Value": "0.0",          "Col3 header": "A5",          "Col3 Value": "0.0",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "0.0",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "0.0",          "Col8 header": "D",          "Col8 Value": "0.0"        },        {          "Parameter": "NHWD",          "Col1 Unit Header": "kg",          "Col1 header": "A1-A3",          "Col1 Value": "22.2E-6",          "Col2 header": "A4",          "Col2 Value": "0.0",          "Col3 header": "A5",          "Col3 Value": "0.0",          "Col4 header": "C1",          "Col4 Value": "0.0",          "Col5 header": "C2",          "Col5 Value": "0.0",          "Col6 header": "C3",          "Col6 Value": "0.0",          "Col7 header": "C4",          "Col7 Value": "0.0",          "Col8 header": "D",          "Col8 Value": "0.0"        }      ]    }  ],  "program-related information and verification": [    {      "Row header": "Third-party verifier",      "Row unit": "Professor Vladimír Kocí"    },    {      "Row header": "EPD verification",      "Row unit": "Third-party verification according to ISO 14025:2006"    }  ]}

相关文章:

  • 蓝桥杯篇---IAP15F2K61S2串口
  • STM32的启动流程
  • 基于STM32的智能路灯节能控制系统
  • 【设计模式】01- 一文理解常用设计模式-“创建型模式”篇
  • vue3 分析总结响应式丢失问题原因(二)
  • 【从0做项目】Java搜索引擎(3)
  • 使用 DeepSeek 生成商城流程图
  • Rasa学习笔记
  • 基于HAL库的按钮实验
  • 【个人开发】deepspeed+Llama-factory 本地数据多卡Lora微调
  • 【AI战略思考15】我对做自媒体视频博主的初步探索和一些思考
  • ubuntu下ollama/vllm两种方式在本地部署Deepseek-R1
  • 数据结构 堆和priority_queue
  • 跨平台数字内容整合策略:提升全域用户体验的关键路径
  • 一键终结环境配置难题:ServBay 1.9 革新 AI 模型本地部署体验
  • 使用 HTML CSS 和 JAVASCRIPT 的黑洞动画
  • 解决IDEA报错:java 找不到符号
  • 鸿蒙应用开发者基础
  • AI前端开发:驶向国际化职业发展快车道
  • 09 解决方案 - 开源机器人+具身智能+AI
  • 牛市早报|今年国内核电项目审批首次开闸,离境退税起退点下调
  • 人民时评:投资于人,促高质量充分就业
  • 巴黎奥运后红土首秀落败,郑钦文止步马德里站次轮
  • 本周看啥|在电影院里听民谣,听摇滚,燥起来吧
  • 因商标近似李小龙形象被裁定无效,真功夫起诉国家知产局,法院判了
  • 特写|“三峡千古情”出圈,一场演出给宜昌留下更多游客