You can convert your curl
command into a PHP script using the curl
functions in PHP. Here's an example of how to do it:
<?php
$apiKey = 'YOUR_OPENAI_API_KEY'; // Replace with your actual OpenAI API key
$data = [
"model" => "gpt-4o",
"messages" => [
[
"role" => "user",
"content" => "Draft a company memo to be distributed to all employees. The memo should cover the following specific points without deviating from the topics mentioned and not writing any fact which is not present here:\n \n Introduction: Remind employees about the upcoming quarterly review scheduled for the last week of April.\n \n Performance Metrics: Clearly state the three key performance indicators (KPIs) that will be assessed during the review: sales targets, customer satisfaction (measured by net promoter score), and process efficiency (measured by average project completion time).\n \n Project Updates: Provide a brief update on the status of the three ongoing company projects:\n \n a. Project Alpha: 75% complete, expected completion by May 30th.\n b. Project Beta: 50% complete, expected completion by June 15th.\n c. Project Gamma: 30% complete, expected completion by July 31st.\n \n Team Recognition: Announce that the Sales Team was the top-performing team of the past quarter and congratulate them for achieving 120% of their target.\n \n Training Opportunities: Inform employees about the upcoming training workshops that will be held in May, including \"Advanced Customer Service\" on May 10th and \"Project Management Essentials\" on May 25th."
]
],
"temperature" => 1,
"max_tokens" => 1024,
"top_p" => 1
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/chat/completions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer $apiKey"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if ($response === false) {
echo 'Error: ' . curl_error($ch);
} else {
echo 'Response: ' . $response;
}
curl_close($ch);
?>
- API Key: Replace
'YOUR_OPENAI_API_KEY'
with your actual OpenAI API key. - cURL setup: We're initializing the cURL session and setting options such as the URL (
https://api.openai.com/v1/chat/completions
), headers (Content-Type and Authorization), and the data we need to send (usingjson_encode
to encode the$data
array). - Sending POST request: We send the request with
CURLOPT_POST
set totrue
and pass the JSON-encoded data. - Handling response: We check if the cURL request was successful and then output the response or an error message.
Let me know if you need further clarification!