Skip to content

Instantly share code, notes, and snippets.

@janus57
Last active December 21, 2023 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janus57/333b8575d9fc049f9b6771fd452eb7af to your computer and use it in GitHub Desktop.
Save janus57/333b8575d9fc049f9b6771fd452eb7af to your computer and use it in GitHub Desktop.
cURL test with PHP into browser
<?php
$USERAGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/115.0';
$URL = false;
$curl_error = false;
$validation = true;
if (!empty($_POST['url']) && filter_var($_POST['url'], FILTER_VALIDATE_URL)) {
$URL = $_POST['url'];
}
elseif (!empty($_POST['url']) && !filter_var($_POST['url'], FILTER_VALIDATE_URL)) {
$validation = false;
}
if ($URL != false) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, true);
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch, CURLOPT_USERAGENT, $USERAGENT);
$output = curl_exec($ch);
$header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($output, $header_len);
if(curl_errno($ch))
{
$curl_error = curl_error($ch);
}
curl_close($ch);
}
?>
<!doctype html>
<html lang="fr">
<head>
<title>cURL test URL</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Petit script pour tester cURL via un navigateur</h1>
<h3>Source : https://gist.github.com/janus57/333b8575d9fc049f9b6771fd452eb7af</h3>
<?php
if ($URL != false && !empty($_POST['submit']) && $curl_error != true)
{
echo '<h2>Contenu de la page extrait via cURL</h2>
<textarea rows="10" cols="200">
'. $body .'
</textarea>
';
}
elseif ($validation === false) {
echo '<h2>URL non valide détecté</h2>';
}
elseif ($curl_error) {
echo '<h2>Erreur cURL</h2>
<textarea rows="3" cols="100">
'. $curl_error .'
</textarea>
';
}
?>
<form method="POST">
URL à tester :<br>
<input type="text" name="url" >
<br>
<input type="submit" name="submit" value="Tester">
</form>
<br />
<button onclick="get_deteil()">Afficher le détail (débug)</button>
<textarea id="debug" rows="10" cols="200" style="display: none;">
<?php print_r($output); ?>
</textarea>
<script>
function get_deteil() {
var x = document.getElementById("debug");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment