Skip to content

Instantly share code, notes, and snippets.

@nekhbet
Created March 16, 2024 19:52
Show Gist options
  • Save nekhbet/7fdbfbeed72bb8a6ef2ca6154d54f3cf to your computer and use it in GitHub Desktop.
Save nekhbet/7fdbfbeed72bb8a6ef2ca6154d54f3cf to your computer and use it in GitHub Desktop.
<?php
function validateURL($url)
{
if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
// Parse the URL to get its components
$parsedUrl = parse_url($url);
// Check the scheme
if (in_array($parsedUrl['scheme'], ['http', 'https'])) {
return true;
}
}
return false;
}
// Check if the 'url' GET parameter is set
if (isset($_GET['url']) && validateURL($_GET['url'])) {
$url = $_GET['url'];
// Initialize a cURL session
$curl = curl_init($url);
// Set cURL options
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Return the transfer as a string
curl_setopt($curl, CURLOPT_HEADER, false); // Do not include the header in the output
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
curl_setopt($curl, CURLOPT_MAXREDIRS, 5); // Maximum number of redirects to follow
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'); // Set a user agent
// Execute the cURL session
$html = curl_exec($curl);
// Check for errors
if (curl_errno($curl)) {
echo 'Curl error: '.curl_error($curl);
} else {
// Output the fetched HTML
echo $html;
}
// Close the cURL session
curl_close($curl);
} else {
echo "No URL provided!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment