| <?php |
| error_reporting(0); |
| |
| ini_set('output_buffering', 'off'); |
|
|
| |
| ini_set('zlib.output_compression', false); |
|
|
| |
| while (@ob_end_flush()) {} |
| |
| |
| header('X-Accel-Buffering: no'); |
|
|
| $url = 'https://api.openai.com/'; |
| $url = rtrim($url, '/'); |
| $headers = getallheaders(); |
| unset($headers['Host']); |
| unset($headers['Content-Length']); |
| $headers['Connection'] = 'close'; |
| $headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'; |
| $targetHeaders = []; |
| foreach ($headers as $key => $value) { |
| if ($key !== 'Host' && $key !== 'Content-Length') { |
| $targetHeaders[] .= ($key . ': ' . $value); |
| } |
| } |
|
|
| header('Access-Control-Allow-Headers: *'); |
| header('Access-Control-Allow-Origin: *'); |
| header('Access-Control-Allow-Methods: *'); |
| header('Access-Control-Max-Age: 86400'); |
|
|
| if($_SERVER['REQUEST_METHOD'] === "OPTIONS"){ |
| exit(); |
| } |
|
|
|
|
| $options = array( |
| 'http' => array( |
| |
| |
| 'method' => $_SERVER['REQUEST_METHOD'], |
| 'header' => $targetHeaders, |
| 'content' => file_get_contents('php://input'), |
| 'ignore_errors' => true |
| ), |
| 'ssl' => array( |
| 'verify_peer' => false, |
| 'verify_peer_name' => false |
| ) |
| ); |
|
|
| $context = stream_context_create($options); |
| |
| if(preg_match('/(https?):\/\/?(.*?$)/', $_SERVER['REQUEST_URI'], $mc)){ |
| if(count($mc)>2){ |
| $url = $mc[1]. '://' .$mc[2]; |
| } |
| } else { |
| echo "URL不正确"; |
| exit(); |
| } |
|
|
| $stream = fopen($url , 'r', false, $context); |
|
|
|
|
| $responseHeaders = []; |
| foreach ($http_response_header as $header) { |
| $headerParts = explode(':', $header, 2); |
| if (count($headerParts) == 2) { |
| $responseHeaders[trim($headerParts[0])] = trim($headerParts[1]); |
| } |
| } |
|
|
| http_response_code(intval(substr($http_response_header[0],9,3))); |
| foreach ($responseHeaders as $name => $value) { |
| if(stripos($name, "access-control-") !== 0) |
| header(str_replace("\r\n", "", $name . ': ' . $value)); |
| } |
|
|
|
|
| $output = fopen('php://output', 'w'); |
|
|
| stream_copy_to_stream($stream, $output); |
| fclose($stream); |
| fclose($output); |