星空软件

 找回密码
 立即注册

[营销干货] chatgpt怎么对接API,通过php实现get访问返回内容

a1934490951 | 2024-2-16 22:46:59 | 显示全部楼层 |阅读模式

很多小伙伴想对接自己的chatgpt机器人,租和香港服务器,然后对接到自己的APP或者软件里面,但是肯定需要一个后端的程序去处理,其实这个工具就足够用了,你提交信息它就会返回对应的内容,下面是代码
<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

// 检查是否有 GET 请求
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    // 获取 GET 请求参数
    $userQuery = isset($_GET['query']) ? $_GET['query'] : '';

    // 替换为你的实际 API 密钥
    $apiKey = '您的key';

    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL => 'https://api.openai.com/v1/chat/completions',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode([
            "model" => "gpt-3.5-turbo",
            "messages" => [
                ["role" => "system", "content" => "你是 ChatGPT,一个友好、有帮助的 AI"],
                ["role" => "user", "content" => $userQuery],
            ],
        ]),
        CURLOPT_HTTPHEADER => [
            'Content-Type: application/json',
            "Authorization: Bearer {$apiKey}",
        ],
    ]);

    $response = curl_exec($curl);

    if (curl_errno($curl)) {
        echo 'Request Error:' . curl_error($curl);
    } else {
        // 解码响应
        $responseDecoded = json_decode($response, true);
        // 只返回内容
        if (isset($responseDecoded['choices'][0]['message']['content'])) {
            echo $responseDecoded['choices'][0]['message']['content'];
        } else {
            echo 'Error: No response content.';
        }
    }

    curl_close($curl); 
} else {
    echo 'Invalid request method.';
}
?>


比如我访问:http://www.xkrj5.com/xxx/ai.php?query=你在干嘛
然后它就会自动返回下图所示的内容
截图202402162249148799.png





上一篇:指纹浏览器下载,模拟微信浏览器环境,自动换IP导入ua
下一篇:discuz标题超过80限制,dz标题字数80改200,解除系统限制教程
回复

使用道具 举报

proshh | 2024-2-16 22:50:13 | 显示全部楼层
这个挺好玩
回复

使用道具 举报

edexidqibu | 2024-2-16 22:50:16 | 显示全部楼层
支持
回复

使用道具 举报

理想小青年 | 2024-2-16 22:50:20 | 显示全部楼层
太好玩了
回复

使用道具 举报

yxycivo | 2024-2-16 22:50:25 | 显示全部楼层
好东西必须支持
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

全站声明【必看】|小黑屋|新库软件 |网站地图

GMT+8, 2024-4-29 04:10 , Processed in 0.062843 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.