$prompt=”…text…”;
$url = 'https://api.openai.com/v1/chat/completions';
$data = [
"model" => "gpt-4o-search-preview",
"web_search_options" => [
"search_context_size" => "low"
],
"messages" => [
["role" => "user",
"content" => $prompt
]
],
"functions" => [
[
"name" => "web_search_preview",
"parameters" => []
]
]
];
$jsonData = json_encode($data);
#
$headers = [
"Authorization: Bearer " . $apiKey,
"Content-Type: application/json",
"Content-Length: " . strlen($jsonData)
];
#
$options = [
"http" => [
"method" => "POST",
"header" => implode("\r\n", $headers),
"content" => $jsonData
]
];
#
$context = stream_context_create($options);
$result = @file_get_contents($url_, false, $context);
if ($result === FALSE) {
…
}else{
$response = json_decode($result, true);
$responseData = $response['choices'][0]['message']['content'];
…..
}