A lightweight PHP script utilizing cURL to interact with the OpenAI API, enabling developers to integrate GPT-powered conversational agents without dependencies.
- PHP 7.0 or higher
- cURL enabled in PHP
Use Git to clone the repository directly to your local machine.
git clone https://github.com/rossxdev/PHP-OpenAI-API.git
Navigate to the repository and use the "Code" button to download a zip file of the project, which you can then extract to your local machine.
Simply navigate to the PHP file in the GitHub repository, view the raw code, and copy/paste it into a new file in your own development environment.
Choose the method that best suits your workflow and proceed to configure your API key and modify your prompts as needed.
-
Configure API Key
Replace
"your-api-key-here"
in the PHP script with your actual API key obtained from OpenAI.$api_key = "your-api-key-here";
-
Modify the Prompt
Replace
'your-prompt-here'
in the PHP script with the prompt you want to use.['role' => 'user', 'content' => 'your-prompt-here']
-
Execute the Script
Run the PHP script using a server or local development environment.
php your-script-name.php
The script returns the model's response as a string. For custom handling, modify the following section in the script:
echo $decoded_response['choices'][0]['message']['content'];
Feel free to fork the project and submit your contributions via a pull request.
This project is open-source and available under the MIT License.
This script utilizes the OpenAI API and may incur costs. Ensure to review the pricing details on OpenAI's Pricing Page.