以下是实现上述功能的PHP代码:

<?php

$proxy_api = 'http://g.11kw.cc/api.php?words=1000&key=';
$gpt_key = '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';

// 生成文章
$article = generateArticle($proxy_api, $gpt_key);

// 判断生成的内容中是否包含特定字符串
if (strpos($article, 'exceeded your current quota') !== false || strpos($article, 'key is associated with a deactivated account') !== false) {
    // 读取a.txt文件的第一行内容
    $replacement = '';
    $file = 'a.txt';
    if (file_exists($file)) {
        $lines = file($file);
        if (!empty($lines)) {
            $replacement = trim($lines[0]);
            // 替换ai_article_gpt.php文件的第14行内容
            replaceLine('ai_article_gpt.php', 14, $replacement);
            // 删除a.txt的第一行内容
            array_shift($lines);
            file_put_contents($file, implode('', $lines));
        }
    }
}

/**
 * 生成文章
 * @param string $proxy_api
 * @param string $gpt_key
 * @return string
 */
function generateArticle($proxy_api, $gpt_key) {
    $url = $proxy_api . $gpt_key;
    return file_get_contents($url);
}

/**
 * 替换文件指定行的内容
 * @param string $file
 * @param int $lineNumber
 * @param string $replacement
 */
function replaceLine($file, $lineNumber, $replacement) {
    $lines = file($file);
    if (!empty($lines) && array_key_exists($lineNumber - 1, $lines)) {
        $lines[$lineNumber - 1] = $replacement . PHP_EOL;
        file_put_contents($file, implode('', $lines));
    }
}
?>

请注意,以上代码中使用了file_get_contents()和file_put_contents()函数操作文件内容。确保您的PHP环境已启用相关函数,并且对文件和目录有足够的权限。

此外,根据您的需求,您需要将ai_article_gpt.php文件的路径和要替换的行号进行相应的修改,并确保a.txt文件存在并且可读写

用PHP写一段代码代码作用是通过自己提供的文章api$proxy_api = httpg11kwccapiphpwords=1000&key=;和key$gpt_key = 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;生成一篇500字文章判断生成的内容中是否有exceeded your current quota和key is assoc

原文地址: https://www.cveoy.top/t/topic/hU5F 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录