Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function creatium_embed($fullpath) {
$ch = curl_init("https://$fullpath");
$embed = '1';
if (isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])) {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$embed = "$protocol://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}
$headers = ["X-Creatium-Embed: $embed"];
// Передаем Cookie посетителя
$cookies = [];
foreach ($_COOKIE as $key => $value) {
$cookies[] = $key . '=' . urlencode($value);
}
$headers[] = 'Cookie: ' . implode(';', $cookies);
// Передаем IP адрес посетителя
if (isset($_SERVER['REMOTE_ADDR'])) {
$headers[] = "X-Forwarded-For: $_SERVER[REMOTE_ADDR]";
}
foreach ($_SERVER as $key => $value) {
if ($key === 'HTTP_HOST' || $key === 'HTTP_COOKIE') continue;
else if (strpos($key, 'HTTP_') === 0) {
// Преобразование строк типа HTTP_KEY_EXAMPLE в Key-Example
$header = implode('-', array_map(
'ucfirst', array_slice(explode('_', strtolower($key)), 1)
));
$headers[] = "$header: $value";
}
}
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) {
$length = strlen($header);
if (strpos($header, ': ') > 0) {
$pass = true;
// Если кодирование отличается, ошибка возникает
if (strpos(strtolower($header), 'transfer-encoding') === 0) $pass = false;
if ($pass) header($header);
}
return $length;
});
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
if (curl_error($ch)) {
print curl_error($ch);
}
curl_close($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment