Skip to content

ReinhardLenz/cURL-session-response-interface-to-GPTchat

Repository files navigation

#chatGPT integration to Web page by php cURL session response . A question can be posed to chatGPT via a php form. Attach txt xlsx type files to the inquiry.

cURL-session-response-interface-to-GPTchat

A lightweight PHP-based interface for sending user questions β€” including optional .txt or .xlsx file attachments β€” to the OpenAI ChatGPT API using cURL. This project is ideal for embedding ChatGPT capabilities into any traditional website or shared hosting environment where Python or Node.js is not available.

The package includes:

A multilingual user-facing question form

A PHP cURL request handler that sends prompts + file contents to ChatGPT

A JSON-based language file for UI translations

Optional MySQL logging for user queries

βœ… Features

Simple HTML/PHP form for entering questions

File upload support (.txt and .xlsx)

Automatic detection of file type

Excel parsing using PhpSpreadsheet

Language selection system via languages_gpt.json

MySQL logging of all user prompts

Clean cURL integration with the OpenAI Chat Completions API

HTML-safe output rendering

πŸ“ File Overview anonymous-question-form-curl.php

Frontend page containing:

A textarea for entering a question

File upload field for .txt or .xlsx

Localization function t1()

Language strings loaded from languages_gpt.json

Form posts to chatgpt-response-handler-curl2.php

chatgpt-response-handler-curl2.php

Backend processor which:

Receives form input

Saves uploaded file into /uploads/

Detects file extension

Extracts text or Excel content

Combines the user question with extracted file content

Sends a cURL POST request to OpenAI's API

Displays ChatGPT's response

Logs user question into a MySQL database

languages_gpt.json

Language file providing UI text for:

English (en)

Finnish (fi)

French (fr)

Russian (ru)

Chinese (cn)

This enables the form to automatically adapt to the visitor's language.

βœ… Requirements

PHP 7.4+

cURL extension enabled

Composer (if using PhpSpreadsheet)

MySQL database (optional, used for logging)

OpenAI API key

πŸ“¦ Installation

Clone the repository

git clone https://github.com/yourname/cURL-session-response-interface-to-GPTchat.git

Install required PHP packages

composer require phpoffice/phpspreadsheet

Configure your file paths Ensure the following directory exists:

/uploads/

Add your OpenAI API key Inside chatgpt-response-handler-curl2.php:

'Authorization: Bearer sk-xxxxxx',

Optional: Configure database connection Edit connect.php to match your MySQL credentials.

🧠 How It Works

User submits a question β†’ PHP receives text + file.

File type is detected (txt or xlsx).

Text is extracted:

.txt β†’ file_get_contents()

.xlsx β†’ PhpSpreadsheet

Server builds a combined prompt:

Context:

User question:

PHP sends this to ChatGPT using a cURL POST request.

Response is printed safely using htmlspecialchars().

🌐 Multilingual Interface

All text strings used in the form come from:

language_json/languages_gpt.json

To translate to a new language, simply add a new section:

"de": { "gpt1": "...", "gpt2": "...", "gpt3": "..." }

πŸ›  Example cURL Request (from the handler) $data = [ "model" => "gpt-4", "messages" => [ ["role" => "user", "content" => $combinedPrompt] ] ];

$ch = curl_init('https://api.openai.com/v1/chat/completions'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json' ]); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $response = curl_exec($ch);

πŸ”’ Security Notes

Always sanitize output using htmlspecialchars() (already implemented)

Never commit your API key to GitHub

Ensure upload directory is not publicly writable

Consider restricting file size and file type in production

βœ… License

MIT License β€” free to use in personal and commercial projects.

πŸ™Œ Contributing

Pull requests are welcome! Feel free to add features, translations, or better error handling.

About

Send question to chatGPT via a php form. Attach txt xlsx type files to the inquiry.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages