php / 66668888.php
edwagbb's picture
Upload 66668888.php
3400cc1
raw
history blame
3.03 kB
<?php
error_reporting(0);
// 这行代码用于关闭输出缓冲。关闭后,脚本的输出将立即发送到浏览器,而不是等待缓冲区填满或脚本执行完毕。
ini_set('output_buffering', 'off');
// 这行代码禁用了 zlib 压缩。通常情况下,启用 zlib 压缩可以减小发送到浏览器的数据量,但对于服务器发送事件来说,实时性更重要,因此需要禁用压缩。
ini_set('zlib.output_compression', false);
// 这行代码使用循环来清空所有当前激活的输出缓冲区。ob_end_flush() 函数会刷新并关闭最内层的输出缓冲区,@ 符号用于抑制可能出现的错误或警告。
while (@ob_end_flush()) {}
// 这行代码设置 HTTP 响应的自定义头部 X-Accel-Buffering 为 no,用于禁用某些代理或 Web 服务器(如 Nginx)的缓冲。
// 这有助于确保服务器发送事件在传输过程中不会受到缓冲影响。
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(
/* 'proxy' => 'tcp://127.0.0.1:7890',
'request_fulluri' => true, */
'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);
//$stream = fopen($url . ($_SERVER['PATH_INFO']?$_SERVER['PATH_INFO']:''/*$_SERVER['REQUEST_URI']*/), 'r', false, $context);
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);