| Server IP : 150.230.34.123 / Your IP : 216.73.217.23 Web Server : LiteSpeed System : Linux amd-instance-20210802-1657 5.15.0-1042-oracle #48~20.04.1-Ubuntu SMP Mon Aug 21 18:27:46 UTC 2023 x86_64 User : ubuntu ( 1001) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /proc/self/cwd/aloysius_backup/ |
Upload File : |
<?php
// fetchOg.php
// Usage: fetchOg.php?url=https://maps.app.goo.gl/...
// Force UTF‑8 output
ini_set('default_charset', 'UTF-8');
mb_internal_encoding("UTF-8");
mb_http_output("UTF-8");
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=utf-8");
// Validate input
$target = isset($_GET['url']) ? trim($_GET['url']) : '';
if ($target === '' || !filter_var($target, FILTER_VALIDATE_URL)) {
http_response_code(400);
echo json_encode(["error" => "Invalid or missing 'url' parameter"], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
exit;
}
// Try fetching HTML with Facebook UA, fallback to Mozilla UA
$html = fetchHtml($target, "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)");
if ($html === null) {
$html = fetchHtml($target, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121 Safari/537.36");
}
if ($html === null) {
http_response_code(502);
echo json_encode(["error" => "Failed to fetch HTML"], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
exit;
}
// Parse OG tags
$meta = parseOpenGraph($html);
// Fallbacks
if (empty($meta['title'])) {
$meta['title'] = parseTitleTag($html);
}
if (!isset($meta['description'])) {
$meta['description'] = null;
}
if (!isset($meta['image'])) {
$meta['image'] = null;
}
// Normalize image URL
$meta['image'] = absolutizeUrl($meta['image'], $target);
// Output JSON
echo json_encode([
"title" => $meta['title'],
"description" => $meta['description'],
"image" => $meta['image'],
"url" => $target,
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
/* ----------------------- Helpers ----------------------- */
function fetchHtml($url, $userAgent) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 12,
CURLOPT_CONNECTTIMEOUT => 8,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_HTTPHEADER => [
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-US,en;q=0.9",
],
]);
$html = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($html === false || $status >= 400) return null;
return $html;
}
function parseOpenGraph($html) {
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' . $html, LIBXML_NOWARNING | LIBXML_NOERROR);
libxml_clear_errors();
$result = ["title" => null, "description" => null, "image" => null];
$xpath = new DOMXPath($dom);
$result["title"] = getMetaContent($xpath, ['og:title', 'twitter:title']);
$result["description"] = getMetaContent($xpath, ['og:description', 'twitter:description', 'description']);
$result["image"] = getMetaContent($xpath, ['og:image', 'twitter:image', 'og:image:url']);
return $result;
}
function getMetaContent(DOMXPath $xpath, array $keys) {
foreach ($keys as $key) {
$query = sprintf('//meta[@property="%s" or @name="%s"]', $key, $key);
$nodes = $xpath->query($query);
if ($nodes && $nodes->length > 0) {
return clean($nodes->item(0)->getAttribute('content'));
}
}
return null;
}
function parseTitleTag($html) {
if (preg_match('/<title[^>]*>(.*?)<\/title>/is', $html, $match)) {
return clean($match[1]);
}
return null;
}
function clean($s) {
$s = mb_convert_encoding($s ?? '', 'UTF-8', 'auto');
$s = html_entity_decode($s, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$s = preg_replace('/\s+/u', ' ', $s);
return trim($s);
}
function absolutizeUrl($maybeUrl, $baseUrl) {
if (!$maybeUrl) return null;
if (preg_match('#^https?://#i', $maybeUrl)) return $maybeUrl;
if (strpos($maybeUrl, '//') === 0) {
$scheme = parse_url($baseUrl, PHP_URL_SCHEME) ?: 'https';
return $scheme . ':' . $maybeUrl;
}
$base = parse_url($baseUrl);
if (!$base || !isset($base['scheme']) || !isset($base['host'])) return $maybeUrl;
$scheme = $base['scheme'];
$host = $base['host'];
$port = isset($base['port']) ? ':' . $base['port'] : '';
$path = isset($base['path']) ? $base['path'] : '/';
$dir = preg_replace('#/[^/]*$#', '/', $path);
$resolved = ($maybeUrl[0] === '/') ? $maybeUrl : $dir . $maybeUrl;
return $scheme . '://' . $host . $port . $resolved;
}