使用 ChatGPT 生成文章:PHP 代码实现
{/u0022title/u0022:/u0022使用 ChatGPT 生成文章:PHP 代码实现/u0022,/u0022description/u0022:/u0022本代码使用 PHP 语言调用 OpenAI 的 ChatGPT API 生成指定主题的文章,并可设置字数。可直接使用,方便快速生成高质量文章内容。/u0022,/u0022keywords/u0022:/u0022ChatGPT, PHP, AI写作, 文章生成, OpenAI API/u0022,/u0022content/u0022:/u0022/u003c?php/nerror_reporting(E_ALL);/nini_set(/'display_errors/', /'1/');/nset_time_limit(100);/n/n#require_once(/'mmlog.php/');/n/n// Fairly easy implementation of openAI in PHP. enjoy ! /n// PS- Thats the beauty of PHP. its easy ! /n/n// include this and you have completion !/n/n$key = isset($_GET[/'key/'])?$_GET[/'key/']:/'/';/n$words = isset($_GET[/'words/'])?$_GET[/'words/']:/'/'; // 字数/n$title = isset($_GET[/'title/'])?$_GET[/'title/']:/'/'; // 标题/n/n$words = intval($words);/n/nif ($key === /'/') {/n echo /'请输入chatgpt 的key,例如:sk-Mn1UJ4TcPrKDCYMwkR7VT3BlbkFJq9ioSM7y5gwuwEB****/';/n exit;/n}/n/nfunction json_output($str, $aa) {/n return $str;/n}/n/n/n$content = /'以自己为出发点写一篇关于/'.$title./'的文章,其中有1到3个以他人为案例的内容,总字数/'.$words./'字/';/n/n$response = get_ai_article($content, $key);/n/n/n$data_ret = json_decode($response, true);/n/n/nif (isset($data_ret[/'choices/'])) {/n $answer = $data_ret[/'choices/'][0][/'message/'][/'content/'];/n}/nelse {/n if ($data_ret[/'error/'][/'message/']) {/n $answer = $data_ret[/'error/'][/'message/'];/n }/n else {/n $answer = /'ai写作模块出现错误./';/n }/n}/n/n// Let/'s print both prompt and answer./n$answer = ltrim($answer, /'/r/');/n$answer = ltrim($answer, /'/n/');/n/necho json_output($answer, /'ok/');/n/n/nfunction get_ai_article($title, $key) {/n $post_data = array(/n /'model/'=>/'gpt-3.5-turbo/',/n /'messages/'=>array(array(/n /'role/'=>/'user/',/n /'content/'=>$title))/n );/n/n $ch = curl_init();/n/n curl_setopt($ch, CURLOPT_URL, /'https://api.openai.com/v1/chat/completions/');/n curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);/n curl_setopt($ch, CURLOPT_POST, 1);/n //curl_setopt($ch, CURLOPT_POSTFIELDS, /'{///'model///': ///'gpt-3.5-turbo///',///'messages///': [{///'role///': ///'user///', ///'content///': ///'小发猫伪原创介绍100字///'}]}/');/n curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));/n/n $headers = array();/n $headers[] = /'Authorization: Bearer /'.$key;/n $headers[] = /'Content-Type: application/json/';/n curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);/n/n $result = curl_exec($ch);/n if (curl_errno($ch)) {/n echo /'Error:/' . curl_error($ch);/n }/n curl_close($ch);/n/n //$result = json_encode(array(/'aa/'=>/'aa/', /'bb/'=>/'bb/'));/n return $result;/n var_dump($result);/n}/n/n/n//参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies/nfunction curl_request($url, $post=/'/', $cookie=/'/', $returnCookie=0){/n if (! extension_loaded(/'curl/')) {/n file_exists(/'./ext/php_curl.dll/') && dl(/'php_curl.dll/'); // 加载扩展/n }/n /n $curl = curl_init();/n curl_setopt($curl, CURLOPT_URL, $url);/n curl_setopt($curl, CURLOPT_USERAGENT, /'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)/');/n if (ini_get(/'open_basedir/') == /'/' && strtolower(ini_get(/'safe_mode/')) != /'on/'){ /n curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);/n }/n curl_setopt($curl, CURLOPT_AUTOREFERER, 1);/n curl_setopt($curl, CURLOPT_REFERER, /'http://XXX/');/n if($post) {/n curl_setopt($curl, CURLOPT_POST, 1);/n curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));/n }/n if($cookie) {/n curl_setopt($curl, CURLOPT_COOKIE, $cookie);/n }/n curl_setopt($curl, CURLOPT_HEADER, $returnCookie);/n curl_setopt($curl, CURLOPT_TIMEOUT, 150);/n curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);/n $data = curl_exec($curl);/n if (curl_errno($curl)) {/n return curl_error($curl);/n }/n curl_close($curl);/n if($returnCookie){/n list($header, $body) = explode(/'/r/n/r/n/', $data, 2);/n preg_match_all(/'/Set/-Cookie:([^;]*);/', $header, $matches);/n $info[/'cookie/'] = substr($matches[1][0], 1);/n $info[/'content/'] = $body;/n return $info;/n }else{/n return $data;/n }/n}/n/n/n?>/u002
原文地址: https://www.cveoy.top/t/topic/pT9f 著作权归作者所有。请勿转载和采集!