本文将介绍如何通过 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" } ]}