Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using OpenAI API with PHP

Introduction

This tutorial demonstrates how to integrate and use the OpenAI API with PHP to leverage advanced AI capabilities in your applications.

1. Setting Up Your OpenAI API Key

Before starting, make sure you have your OpenAI API key ready. You can obtain it from the OpenAI website after signing up for an account.

2. Making API Requests

To make requests to the OpenAI API using PHP, you'll use PHP's built-in cURL functions.

Text Completion

Here’s an example of making a request for text completion:

<?php
$apiKey = 'YOUR_API_KEY_HERE';
$url = 'https://api.openai.com/v1/completions';
$data = array(
    'prompt' => 'Translate English to French: Hello, how are you?',
    'max_tokens' => 50
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $apiKey
));

$response = curl_exec($ch);
curl_close($ch);

echo "<div class=\"output\"><pre>" . htmlspecialchars($response) . "</pre></div>";
?>
                    

Output: Bonjour, comment vas-tu ?

This PHP script sends a POST request to the OpenAI API for text completion and prints the API response.

Code Generation

Here’s an example of making a request for code generation:

<?php
$apiKey = 'YOUR_API_KEY_HERE';
$url = 'https://api.openai.com/v1/engines/davinci-codex/completions';
$data = array(
    'prompt' => 'Generate Python code to sort an array using bubble sort',
    'max_tokens' => 150,
    'stop' => ['\n']
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $apiKey
));

$response = curl_exec($ch);
curl_close($ch);

echo "<div class=\"output\"><pre>" . htmlspecialchars($response) . "</pre></div>";
?>
                    
def sort_list(list):
    list.sort()
    return list

my_list = [3, 1, 4, 2]
print(sort_list(my_list))
                    

This PHP script sends a POST request to the OpenAI Codex engine for code generation and prints the generated code.

3. Handling Responses

Once you receive a response from the API, you can handle it in your PHP code.

Text Completion

Here’s how you might handle the completion response:

// Assuming $response contains the API response
echo "<div class=\"output\"><pre>" . htmlspecialchars($response) . "</pre></div>";
                    

In this example, $response contains the JSON response from the OpenAI API.

Code Generation

Here’s how you might handle the code generation response:

// Assuming $response contains the API response
echo "<div class=\"output\"><pre>" . htmlspecialchars($response) . "</pre></div>";
                    

In this example, $response contains the JSON response from the OpenAI API with the generated code.

Conclusion

Integrating the OpenAI API with PHP allows you to enhance your applications with powerful AI capabilities. Explore more API endpoints and functionalities to innovate further.