Skip to content

Instantly share code, notes, and snippets.

@janus57 janus57/curl_test.php
Last active May 15, 2018

Embed
What would you like to do?
cURL test with PHP into browser
<?php
$URL = false;
$curl_error = false;
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);
$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>
<?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
You can’t perform that action at this time.